[英]Angular router.navigateByUrl() not working if page is still loading
I am trying to integrate auth0 into my angular app. 我正在尝试将auth0集成到我的角度应用程序中。
Auth0 login event flow happens like below. Auth0登录事件流程如下所示。
Angular page shows the auth0 login widget (The code for showing the login widget is located inside a service: AuthService
). 角度页面显示auth0登录小部件(用于显示登录小部件的代码位于服务内部:
AuthService
)。
User picks the authentication provider from the login widget (eg: login with google). 用户从登录小部件中选择身份验证提供程序(例如:使用google登录)。
Browser gets redirected to the authentication provider. 浏览器被重定向到身份验证提供程序。
User enters credentials in the authentication provider. 用户在身份验证提供程序中输入凭据。
Browser gets redirected back to the angular site, to the configured callback URL. 浏览器将重定向回到有角度的站点,再配置的回调URL。 In my case, this is a page which shows a loading indicator (since login is still not complete).
就我而言,这是一个页面,其中显示了加载指示符(因为登录仍未完成)。
A callback event gets fired from the auth0 login widget with the authentication token. 使用身份验证令牌从auth0登录小部件触发回调事件。
Inside the callback, A REST call is made to get more information about the user. 在回调内部,进行REST调用以获取有关用户的更多信息。
When the REST call finishes, router.navigateByUrl('/home')
is called to return to home, after saving retrieved user information to local storage. REST调用完成后,在将检索到的用户信息保存到本地存储中之后,将调用
router.navigateByUrl('/home')
以返回主目录。
But, when I call router.navigateByUrl('/home')
, the navigation doesn't happen. 但是,当我调用
router.navigateByUrl('/home')
,导航不会发生。 But, if I call router.navigateByUrl('/home')
after about a 5 second timeout, the navigation happens successfully. 但是,如果我在大约5秒钟的超时后调用
router.navigateByUrl('/home')
,则导航成功完成。 I suspect that for the navigation to happen, browser loading should have completed when the navigateByUrl()
call is made. 我怀疑要进行导航,应该在进行navigationByUrl
navigateByUrl()
调用时完成浏览器加载。
Does anyone know a way to do the navigation reliably, without using a timeout? 有谁知道可靠地进行导航而不用超时的方法?
Following is the AuthService
code. 以下是
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,现在可以正常使用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.