【问题标题】:Angular router does not react to Cognito redirect?Angular 路由器对 Cognito 重定向没有反应?
【发布时间】:2020-02-27 00:13:24
【问题描述】:

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

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

路由器配置如下:

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

【问题讨论】:

    标签: javascript angular angular-routing angular-router aws-amplify


    【解决方案1】:

    想通了。即使用户在使用 Google 登录后通过了身份验证,但第一次调用类似于此帖子签名:

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

    记录NOT AUTHENTICATED YET。所以AuthGuard不允许用户通过。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-28
      • 2020-10-22
      • 1970-01-01
      • 1970-01-01
      • 2018-08-13
      • 1970-01-01
      • 2017-09-17
      • 2016-11-18
      相关资源
      最近更新 更多