错误:用户未在 NodeJS 中通过 amazon cognito 身份验证

Rah*_*hul 5 amazon-web-services node.js amazon-cognito

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

我做了什么

首先,我使用userpool.signUp(..)根据文档的方法进行注册。cognitoUser.confirmRegistration(..)然后我根据文档使用方法确认了验证码。

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

我遇到了一个 SO问题,我得到getCurrentUser()will return null at backend side。

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

Dav*_*tiz 5

您必须首先调用该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")
    },
  })
}
Run Code Online (Sandbox Code Playgroud)

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