简体   繁体   English

错误:用户未在 nodejs 中对 amazon cognito 进行身份验证

[英]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-jsaws-sdk在我的 nodeJs 应用程序中设置身份验证。

What i did我做了什么

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.

相关问题 Cognito 用户更新属性获取错误 - 错误:用户未通过身份验证 - Cognito user update attribute getting error - Error: User is not authenticated 从 Android 在 Amazon Cognito 中启用用户 mfa - Enable user mfa in Amazon Cognito from Android 如何禁用 Amazon Cognito 中某些用户的验证? - How to disable verification for some user in Amazon Cognito? Aws cognito 通过管理员 nodejs 创建用户 - Aws cognito create user by admin nodejs 在预身份验证用户的情况下如何使用 amazon cognito 获取刷新令牌 - How to get refresh token using amazon cognito in case of Preauthenticated User amazon-cognito-identity-js - 忘记了无效用户的密码 - amazon-cognito-identity-js - forgot password for Invalid User 用于登录 Amazon Cognito 用户池的 REST(或 CLI)API 是什么 - What is the REST (or CLI) API for logging in to Amazon Cognito user pools 使用 amazon-cognito-identity.min.js 获取 Cognito 用户自定义属性 - Getting Cognito User custom attributes with amazon-cognito-identity.min.js 在 AWS Cognito PostAuthentication 触发器事件处理程序中自动获取经过身份验证的用户的 AccessToken lambda - Automatically getting AccessToken of authenticated-user in AWS Cognito PostAuthentication trigger event handler lambda AWS Cognito:在 Auth.signIn 中出现错误(验证 amazon-cognito-identity-js 已链接) - AWS Cognito: Getting error in Auth.signIn (Validate that amazon-cognito-identity-js has been linked)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM