【问题标题】:Error: User is not authenticated amazon cognito in nodejs错误:用户未在 nodejs 中通过 amazon cognito 身份验证
【发布时间】:2021-05-04 01:25:38
【问题描述】:

我正在使用 amazon-Cognito-identity-jsaws-sdk 在我的 nodeJs 应用程序中设置身份验证。

我做了什么

首先我根据docs使用userpool.signUp(..)方法注册。 然后我根据docs使用cognitoUser.confirmRegistration(..)方法确认验证码。

到这里为止,一切正常。但是,现在我想获取用户的所有属性。我找到了一个方法cognitoUser.getUserAttributes(..),它可以给我属性。但是当我在cognitoUser.confirmRegistration(..) 之后调用此方法时,它会返回 Error: User is not authenticated

我经历了一个 SO question 在那里我得到了 getCurrentUser() 将在后端返回 null。

那么,我如何在完全安全的后端使用 nodejs 授权任何用户?

【问题讨论】:

    标签: node.js amazon-web-services amazon-cognito


    【解决方案1】:

    你必须先调用authenticateUser方法:

    import { CognitoUser, AuthenticationDetails } from 'amazon-cognito-identity-js'
    
    const authUser = () => {
      const cognitoUser = new CognitoUser({ Username, Pool })
      const authDetails = new AuthenticationDetails({
        Username,
        Password,
      })
      cognitoUser.authenticateUser(authDetails, {
        onSuccess: () => {
          console.log("User authenticated")
        },
        onFailure: (error) => {
          console.log("An error happened")
        },
      })
    }
    

    之后,getCurrentUser 方法将返回当前登录的用户。您也可以调用getUserAttributes 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-17
      • 1970-01-01
      • 2018-05-08
      • 1970-01-01
      • 2017-01-02
      相关资源
      最近更新 更多