问题
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.
回答1:
I've been working through this same issue.. It seems you have a couple options.
a) Use popup mode
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
}
}
Popup Mode Docs
b) Save state in LocalStorage
Auth0 suggests saving state to LocalStorage if you want to allow users to resume where they left off.
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']);
}
});
来源:https://stackoverflow.com/questions/39199897/auth0-redirecting-back-to-call-url-after-login-angular2