【发布时间】:2020-06-11 16:17:00
【问题描述】:
In this article作者在AuthGuard中使用Auth.currentAuthenticatedUser()是这样的:
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return Auth.currentAuthenticatedUser().then(() => { return true; })
.catch(() => {
this.router.navigate(['signin']);
return false;
});
}
但是 as noted in this issue 即使使用已通过身份验证并已被重定向,这也会抛出,从而导致两次重定向。第一个来自 Cognito,第二个来自 auth guard,因为它还没有“认为”用户已经登录。
那么我们应该在Auth 上调用什么来保证在用户通过联合身份登录时不会抛出?
我认为Auth.currentSession() 会起作用,但想仔细检查一下。
更新
我尝试了Auth.currentSession(),但它也没有在重定向后立即提供会话。
这是我在AppComponent中尝试过的:
Auth.currentSession().
then((s) => console.log(`The current session is ${JSON.stringify(s)}`)).
catch((e) => console.log(`There is no current session ${e}`))
在应用程序加载时重定向后记录如下:
There is no current session No current user
如果我手动刷新,它会按照我们的预期记录会话。
【问题讨论】:
-
首先,我会避免在警卫中这样做:
this.router.navigate(['signin'])。返回UrlTree是这里的预期用途(在我看来)。
标签: javascript angular amazon-web-services amazon-cognito aws-amplify