[英]Error: User is not authenticated amazon cognito in nodejs
I am using amazon-Cognito-identity-js
and aws-sdk
to setup authentication in my nodeJs app.我正在使用amazon-Cognito-identity-js
和aws-sdk
在我的 nodeJs 应用程序中设置身份验证。
First i signup using userpool.signUp(..)
method according to docs .首先,我根据文档使用userpool.signUp(..)
方法注册。 Then i confirmed the verification code using cognitoUser.confirmRegistration(..)
method according to docs .然后我根据文档使用cognitoUser.confirmRegistration(..)
方法确认了验证码。
Up to here, everything works fine.到这里为止,一切正常。 But, now i would like to get all attributes of the user.但是,现在我想获取用户的所有属性。 I found a method cognitoUser.getUserAttributes(..)
which can give me attributes.我找到了一个可以给我属性的方法cognitoUser.getUserAttributes(..)
。 But when i call this method after cognitoUser.confirmRegistration(..)
then it returns Error: User is not authenticated .但是当我在cognitoUser.confirmRegistration(..)
之后调用此方法时,它会返回Error: User is not authenticated 。
I went through a SO question where I got
getCurrentUser()
will return null at backend side.我经历了一个 SO 问题,在那里我得到了getCurrentUser()
will return null at backend side。
So, how can I authorize any user at the fully secured backend side using nodejs?那么,我如何使用 nodejs 在完全安全的后端授权任何用户?
You have to first call the authenticateUser
method:您必须先调用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")
},
})
}
After that, the getCurrentUser
method will return the current logged in user.之后, getCurrentUser
方法将返回当前登录的用户。 And you will be able to call the getUserAttributes
method as well.您还可以调用getUserAttributes
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.