【问题标题】:Amplify - How to setup congnito to use email as username?Amplify - 如何设置 cognito 以使用电子邮件作为用户名?
【发布时间】:2019-10-03 13:54:19
【问题描述】:

使用 AWS Amplify 通过 Reactjs 创建身份验证 (Cognito),默认用户名是 UUID。所以当我登录系统时,我看到:

你好 f725e4a2-1f24-487d-9265-864947f5c5eb

如何改变这种行为?可能有一种方法可以在前端进行更改,但我想在后端进行更改,因为 Cognito 中的用户列表也有 UUID 作为标识符。我希望将电子邮件视为用户名。

谢谢!

【问题讨论】:

  • 您想将用户名值从 UUID 更改为电子邮件?我
  • 是的!这就是我的朋友!

标签: reactjs aws-amplify


【解决方案1】:

您必须创建一个新的用户池,因为在创建池后您无法更新此类设置。

转到控制台并直接在 Web 上配置一个新用户池,它将为您提供使用电子邮件选项登录

然后使用新的用户池详细信息更新到您的 Auth 配置。

来自aws-amplify的示例

import Amplify, { Auth } from 'aws-amplify';

Amplify.configure({
    Auth: {

        // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
        identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',

        // REQUIRED - Amazon Cognito Region
        region: 'XX-XXXX-X',

        // OPTIONAL - Amazon Cognito Federated Identity Pool Region 
        // Required only if it's different from Amazon Cognito Region
        identityPoolRegion: 'XX-XXXX-X',

        // OPTIONAL - Amazon Cognito User Pool ID
        userPoolId: 'XX-XXXX-X_abcd1234',

        // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
        userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3',

        // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
        mandatorySignIn: false,

        // OPTIONAL - Configuration for cookie storage
        // Note: if the secure flag is set to true, then the cookie transmission requires a secure protocol
        cookieStorage: {
        // REQUIRED - Cookie domain (only required if cookieStorage is provided)
            domain: '.yourdomain.com',
        // OPTIONAL - Cookie path
            path: '/',
        // OPTIONAL - Cookie expiration in days
            expires: 365,
        // OPTIONAL - Cookie secure flag
        // Either true or false, indicating if the cookie transmission requires a secure protocol (https).
            secure: true
        },

        // OPTIONAL - customized storage object
        storage: new MyStorage(),

        // OPTIONAL - Manually set the authentication flow type. Default is 'USER_SRP_AUTH'
        authenticationFlowType: 'USER_PASSWORD_AUTH'
    }
});

// You can get the current config object
const currentConfig = Auth.configure();

或者使用新的池详细信息直接更新aws-exports.js 文件。

如果您的应用程序已经上线,您可能需要迁移您的用户。

How do I change the attributes of an Amazon Cognito user pool after creation 会是一个很好的向导

【讨论】:

    【解决方案2】:

    目前无法在创建用户池后对其进行更新。

    在放大文件夹中,您将看到一个文件夹 awscloudformation,其中包含一个文件“nested-cloudformation-stack.yml”

    在文件中搜索此“usernameAttributes”并将其替换为电子邮件而不是用户名

    "usernameAttributes": "email",
    

    另外,您只能在创建认知池之前设置它

    【讨论】:

      【解决方案3】:

      但是@Gustavo Sooeiro 解决方法是什么?我个人勾选了正确的框,但我到处都有用户名的 UUID

      【讨论】:

      • 这并不能真正回答问题。如果您有其他问题,可以点击 进行提问。要在此问题有新答案时收到通知,您可以follow this question。一旦你有足够的reputation,你也可以add a bounty 来引起对这个问题的更多关注。 - From Review
      猜你喜欢
      • 2018-01-24
      • 2020-03-09
      • 2021-05-13
      • 2020-12-09
      • 2017-07-19
      • 2019-08-29
      • 2021-09-10
      • 2015-10-15
      • 2012-02-06
      相关资源
      最近更新 更多