Auth0 redirecting back to call url after login angular2

冷暖自知 提交于 2019-12-11 08:11:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!