amazon-web-services - AWS Cognito 托管 UI 和 Amplify Auth 与授权代码 OAuth 流

标签 amazon-web-services vue.js amazon-cognito aws-amplify

我有一个 Vue.js Web 应用程序,我正在尝试使用 AWS Cognito 和 Amplify Auth 添加简单的身份验证。我的用户池设置了为 OAuth 流程启用的“授权代码授予”。我还将重定向 URL 设置为 https://example.auth.us-east-2.amazoncognito.com/login?response_type=code&client_id=XXXXXXXX&redirect_uri=https://example.com/auth/verify用于托管 UI。

这是托管 UI 重定向到的页面中的内容:

import { Auth } from "aws-amplify";

export default {
    async created() {
        try {
            await Auth.currentSession();
        } catch {
            console.error("Not authorized");
        }
    }
}

当我第一次通过托管 UI 登录并被重定向时,我收到一个错误,并且 Amplify 未将其识别为已通过身份验证。但是,如果我第二次登录,控制台中没有错误并且我有一个经过身份验证的 session 。

我确实知道授权代码授权不会将 token 放入 URL,但即使在第一次登录时我也确实在 localstorage 中看到它们。我尝试切换到使用“ token ”OAuth 流,但 Amplify 文档说刷新 token 不是这样提供的,我不希望 session 限制为 1 小时。这里有什么指导吗?

最佳答案

对于任何面临同样问题的人来说,这似乎是一个已知问题。

解决方法是订阅集线器操作并在那里处理它

Hub.listen("auth", ({ payload: { event, data } }) => {
  switch (event) {
  case "signIn":
    // signin actions
    Auth.currentSession()
      .then(user => console.log(user)) // redirect to default page
      .error(err => console.log(err))
  case "signOut":
    // signout actions, redirect to '/' etc
  case "customOAuthState":
    // other changes
  }
}

引用 https://github.com/aws-amplify/amplify-js/issues/5133#issuecomment-600759135

关于amazon-web-services - AWS Cognito 托管 UI 和 Amplify Auth 与授权代码 OAuth 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60444242/

相关文章:

amazon-web-services - 我们如何在 docker 上运行 Neptune 图数据库

vue.js - 我如何使用 Vuelidate 来验证 <select> 输入

amazon-web-services - 具有无服务器本地环境的 AWS Cognito

amazon-ec2 - 在实例启动时自动启动 apache - aws autoscaling

Java AWS DynamoDB如何增加数字

laravel - 在 laravel 5.5/vue.js 应用程序中使用 jquery-confirm 库

amazon-web-services - 如何自动化 aws 托管 UI 的 Logo

aws-api-gateway - API Gateway 何时验证撤销的 Cognito ID token

javascript - 带有 Promise 的 AWS Javascript SDK

reactjs - 在 vue.js 组件中响应 this.props.children