登入angular2后,Auth0重新导向回呼叫网址 [英] Auth0 redirecting back to call url after login angular2
问题描述
登录后,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);
}
然后在您的auth回调中:
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重新导向回呼叫网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!