我正在尝试将auth0集成到我的角度应用程序中。
Auth0登录事件流程如下所示。
AuthService
)。router.navigateByUrl('/home')
返回到home。但是,当我打电话给router.navigateByUrl('/home')
时,导航不会发生。但是,如果我在大约5秒钟超时后调用router.navigateByUrl('/home')
,则导航成功。我怀疑为了导航发生,浏览器加载应该在进行navigateByUrl()
调用时完成。
有没有人知道如何可靠地进行导航,而不使用超时?
以下是AuthService
代码。
@Injectable()
export class AuthService {
lock = new Auth0Lock(
'my_client_id',
'my_auth0_sub_domain',
{
auth: {
redirectUrl: window.location.origin + '/auth_loading',
responseType: 'token',
scope: 'openid'
}
}
);
userProfile: Object;
constructor(public router: Router) {
this.userProfile = JSON.parse(localStorage.getItem('profile'));
this.lock.on("authenticated", (authResult: any) => {
localStorage.setItem('id_token', authResult.idToken);
this.lock.getProfile(authResult.idToken, (error: any, profile: any) => {
if (error) {
alert(error);
return;
}
localStorage.setItem('profile', JSON.stringify(profile));
this.userProfile = profile;
setTimeout(() => {
this.router.navigateByUrl('/home'); //This only works because of the timeout
}, 5000);
});
});
}
public login() {
this.lock.show(); //This shows the auth0 login widget
}
}
我将auth0-lock
从v10升级到了v11,它现在没有问题。