简体   繁体   English

Cognito 用户池“用户名”显示为 ID 而不是电子邮件,如何解决?

[英]Cognito user pool "username" appears as id not email, how to fix it?

I'm using Amazon Cognito user pools, and i choose to have users signup/In with their emails.我正在使用 Amazon Cognito 用户池,并且我选择让用户使用他们的电子邮件进行注册/登录。 According to online guides, when choosing so, the user pool should list users with "username" value as their email, but this is not the case, i'm seeing the "id" which is also referred to as "sub" as the "username" field!根据在线指南,选择时,用户池应将具有“用户名”值的用户列为他们的电子邮件,但事实并非如此,我看到的“id”也称为“子”作为“用户名”字段! it has the UUID format.它具有 UUID 格式。

Any ideas how to get username shows the email?任何想法如何获取用户名显示电子邮件? ** Note: I'm talking about showing users from AWS cognito console. ** 注意:我说的是从 AWS Cognito 控制台显示用户。 Attached is a screenshot附上截图

在此处输入图片说明

That seems to happen when you chose to signup with either email or phone.当您选择使用电子邮件或电话注册时,似乎会发生这种情况。 Apparently in that configuration Cognito generates 'username' using the sub...显然,在该配置中,Cognito 使用子...

I would suggest configuring Congito to use 'username' and then on signup set up the 'username' to email, you can also pass email (and phone) as additional attributes.我建议将 Congito 配置为使用“用户名”,然后在注册时将“用户名”设置为电子邮件,您还可以将电子邮件(和电话)作为附加属性传递。

I hope that helps我希望有帮助

@Luke is correct , this only happens when you select the "Email address or phone number" option in the "How do you want your end users to sign in" section of the wizard. @Luke 是正确的,这只发生在您在向导的“您希望最终用户如何登录”部分中选择“电子邮件地址或电话号码”选项时。

It's a bit confusing because the SignUp API expects an email-formatted username during signup, but then ends up creating a user with a GUID as a username, and assigns the submitted username to the email attribute.这有点令人困惑,因为 SignUp API 在注册期间需要一个电子邮件格式的用户名,但最终创建了一个具有 GUID 作为用户名的用户,并将提交的用户名分配给电子邮件属性。 This is described in the docs :这在文档中描述:

  • Call the SignUp API and pass an email address or phone number in the username parameter of the API.调用 SignUp API 并在 API 的 username 参数中传递电子邮件地址或电话号码。 This API does the following:此 API 执行以下操作:
    • If the username string is in valid email format, the user pool automatically populates the email attribute of the user with the username value.如果用户名字符串是有效的电子邮件格式,则用户池会自动使用用户名值填充用户的电子邮件属性。
    • If the username string format is not in email or phone number format, the SignUp API throws an exception.如果用户名字符串格式不是电子邮件或电话号码格式,SignUp API 将引发异常。
    • The SignUp API generates a persistent UUID for your user, and uses it as the immutable username attribute internally. SignUp API 为您的用户生成一个持久的 UUID,并在内部将其用作不可变的用户名属性。 This UUID has the same value as the sub claim in the user identity token.此 UUID 与用户身份令牌中的 sub 声明具有相同的值。
    • If the username string contains an email address or phone number that is already in use, the SignUp API throws an exception.如果用户名字符串包含已在使用的电子邮件地址或电话号码,SignUp API 将引发异常。

This is in fact probably what you want though, as it allows users to change their emails down the line (as actual usernames cannot be changed).这实际上可能是您想要的,因为它允许用户更改他们的电子邮件(因为无法更改实际用户名)。 And furthermore, the docs state that "You can use an email address or phone number as an alias in place of the username in all APIs except the ListUsers API", so the fact that the username is a GUID in the backend doesn't have much effect on how you interact with the API.此外, 文档指出“您可以在除 ListUsers API 之外的所有 API 中使用电子邮件地址或电话号码作为别名来代替用户名”,因此用户名是后端 GUID 的事实没有对您与 API 交互的方式有很大影响。

As for listing the email in the console, it looks like they've added an email column in the user list, so that shouldn't be a problem anymore.至于在控制台中列出电子邮件,看起来他们在用户列表中添加了一个电子邮件列,所以这应该不再是问题了。

Here you have an example.这里有一个例子。

auth.service.ts auth.service.ts

you have to do the login method for Cognito in your authentication service.您必须在身份验证服务中为 Cognito 执行登录方法。 Something like this:像这样的东西:

login(username: string, password: string): Promise<CognitoUser|any> {
    return new Promise((resolve,reject) => {
      Auth.signIn(username,password)
      .then((user: CognitoUser|any) => {
        this.currentUserSubject.next(user);
        localStorage.setItem("currentUser", JSON.stringify(user));
        localStorage.setItem("token", user.signInUserSession.idToken.jwtToken);
        localStorage.setItem("userGroup", user.signInUserSession.idToken.payload['cognito:groups']);
        localStorage.setItem("userEmail", user.attributes.email);
        this.loggedIn = true;
        resolve(user);
      }).catch((error: any) => reject(error));
    });
  }

and then in the .ts of your component you have to do the call of that method in the constructor and store in a variable the userEmail argument that you get in the localStorage from Cognito:然后在组件的 .ts 中,您必须在构造函数中调用该方法,并将从 Cognito 在 localStorage 中获取的 userEmail 参数存储在变量中:

constructor(
    private authService: AuthService,
  ) {
    this.authService.currentUser.subscribe(response => {
      this.currentUser = response;
    });

    this.userSessionName = localStorage.getItem('userEmail');
  }

Finally, you have to display the userSessionName variable in you .html component:最后,您必须在 .html 组件中显示 userSessionName 变量:

<p>{{ userSessionName }}</p>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户名不能为电子邮件格式,因为已为电子邮件配置了用户池 - Username cannot be of email format, since user pool is configured for email AWS - 在现有 React web 应用程序上使用用户名和密码从现有 Cognito 用户池获取用户令牌 - AWS - Get user token from an existing Cognito User Pool with username and password on an existing React web app 在 cognito aws 上更改 email 用户名 - change email username on cognito aws 如何以编程方式在Cognito用户池中创建用户? - How can you programmatically create a user in a Cognito User Pool? 当错误消息说用户名和密码不匹配时,如何通过Cognito对用户池中的用户进行身份验证? - How do you authenticate a user in a user pool via Cognito when error messages say userName and password don't match even though they do? AWS Cognito 注册和登录 - 如何存储池和应用程序 ID - AWS cognito registration and login - how to store pool and app id 如何针对AWS Cognito用户池进行身份验证 - How do I authenticate against an AWS Cognito User Pool 使用 Cognito 用户池凭据对用户进行身份验证 - Authenticate user with Cognito user pool credentials 如何在不验证电子邮件或电话的情况下确认 Cognito 用户池中的用户? - How to confirm user in Cognito User Pools without verifying email or phone? Email 和在 React 应用程序中使用 firebase 的密码注册,用户的显示名称在初始渲染中显示为 null。 我怎样才能解决这个问题? - Email and Password Signup using firebase in a react app , the displayName of the user appears to be null on the initial render. How can I fix this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM