问题描述
登录后,auth0 将我重定向回 angular2 上的回调 URL.我怎样才能让它转到我给路由的路径而不重定向到回调 url.
After login the auth0 redirects me back to the call back url on angular2. How can i make it go to the path that i gave to the routing without redirecting to the call back url.
推荐答案
我一直在解决同样的问题.. 看来你有几个选择.
I've been working through this same issue.. It seems you have a couple options.
这很容易实现,但由于浏览器不一致,Auth0 建议不要使用它.(即一些 IE 和 Chrome/Firefox在 iOS 上)
This is easy to implement but Auth0 recommends to not use this because of browser inconsistencies. (namely some IE and Chrome/Firefox on iOS)
就像在锁定选项对象中添加一个标志一样简单
It's as simple as adding a flag to your lock options object
var lockOptions = {
auth: {
redirect: false
}
}
Auth0 建议将状态保存到 LocalStorage,如果您想让用户从上次中断的地方继续.
Auth0 suggests saving state to LocalStorage if you want to allow users to resume where they left off.
因此,就在触发 lock.show()
之前,您可以将当前 URL/路径/状态/等保存到本地存储中.就我而言,我使用的是 authGuard.
So, just before triggering lock.show()
, you could save the current URL/path/state/etc into local storage. In my case, I'm using an authGuard.
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
if (this.authService.isLoggedIn) {
return Promise.resolve(true);
}
// Store the attempted URL for redirecting after auth
localStorage.setItem('redirectUrl', state.url);
// Navigate to the login page
this.router.navigate([`${environment.loggedOutRoute}`]);
return Promise.resolve(false);
}
然后在您的身份验证回调中:
Then inside your auth callback:
// On authentication
this.lock.on('authenticated', (authResult: any) => {
// Save the token in LS
localStorage.setItem('token', authResult.idToken);
// Hide the login modal
this.lock.hide();
// Redirect the user
const redirectUrl: string = localStorage.getItem('redirectUrl');
if (redirectUrl) {
this.router.navigate([redirectUrl]);
} else {
this.router.navigate(['/overview']);
}
});
这篇关于登录angular2后Auth0重定向回调用url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!