Angular 路由器对 Cognito 重定向没有反应?

Angular router does not react to Cognito redirect?

当我们使用 AWS Cognito 进行社交登录时,Cognito 会向浏览器发送重定向以转到 signin redirect URL post 登录。在这种情况下配置的 URL 是 http://localhost:4200/home/.

当应用程序收到此重定向时,它会转到 http://localhost:4200/home/,但显示的组件仍然是 http:localhost:4200/signin/ 的组件。如果我通过选择 URL 并按回车键手动刷新页面,则会显示主页组件。

路由器配置如下所示:

const routes: Routes = [
  { path: '', pathMatch: 'full', redirectTo: 'signin' },
  { path: 'home', component: HomeComponent, canActivate: [ AuthGuard ] },
  { path: 'signin', component: SigninComponent, canActivate: [ UnauthGuard ] },
  { path: 'signout', component: SignoutComponent, canActivate: [ UnauthGuard ] }
]

所以应用程序总是首先重定向到 signin。问题似乎是双重重定向。

关于如何解决这个问题的想法?

我在没有 Router 重定向的情况下尝试过。我添加了一个与 '' 路由一起显示的 welcome 页面。单击此页面上的登录时,应用程序最初仍会重新加载 signin 路由(它停止的地方)并重定向到 home 路由,但登录组件仍会显示。

const routes: Routes = [
  { path: '', component: WelcomeComponent, canActivate: [ UnauthGuard ] },
  { path: 'home', component: HomeComponent, canActivate: [ AuthGuard ] },
  { path: 'signin', component: SigninComponent, canActivate: [ UnauthGuard ] },
  { path: 'signout', component: SignoutComponent, canActivate: [ UnauthGuard ] }
]


更新

查看答案。提交错误报告:

https://github.com/aws-amplify/amplify-js/issues/4988

想通了。即使用户在使用 Google 登录后通过了身份验证,第一次调用也是如此 post signing:

    Auth.currentAuthenticatedUser()
      .then((u) => {
        console.log('The user is : ', u)
      }).catch(e=>{console.log("NOT AUTHENTICATED YET")})
  }

日志NOT AUTHENTICATED YET。所以AuthGuard不允许用户通过。

当页面再次刷新时 Auth.currentAuthenticatedUser() return 用户,因此守卫将应用程序定向到正确的页面。