Skip to content

How can I add all three alias attributes in AWS Cognito while setting up Amplify Gen2 Auth?

0

I'm using an AWS Amplify React application with Gen2. When configuring authentication through the resources.ts file, it only allows me to select one or two sign-in attributes (e.g., email, phone number, or username).

Amplify Auth Gen2

However, in the AWS Cognito console, I can select all three attributes. I want users to log in using their email, phone number, or username.

AWS Cognito Console

How can I achieve this in Amplify Gen2, or is there an alternative approach?

2 Answers
1
Accepted Answer

Hi,

As of now Amplify Gen 2 doesn't support Alias attribute (Username) due to which only Email and Phone Number can be used as sign-in attributes.

According to Amplify Gen 2 documentation, "Amplify Auth does not support signing in with only username and password, however can be configured to enable usernames for display purposes".

[+] https://docs.amplify.aws/react/build-a-backend/auth/concepts/usernames/

There is already a internal feature request with Amplify team to support Alias attribute (Username) with Amplify Gen 2.

Therefore to remediate this issue we would recommend to continue using Amplify Gen 1 as it is supported there.

We are unable to provide an ETA as to when this feature will become available as we are not made privy of development roadmap of future releases. But I'd advise keeping an eye on our "What's New" page and our Official Blog to stay up to date on feature releases.

[+] What's New: https://aws.amazon.com/new/

[+] Official Blog: https://aws.amazon.com/blogs/aws/

AWS
SUPPORT ENGINEER
answered 9 months ago
AWS
EXPERT
reviewed 8 months ago
0

In Amplify Gen2, you can configure multiple sign-in options for your users by modifying the auth resource definition in your resource.ts file. To allow users to sign in with email, phone number, and username, you need to use the loginWith and userAttributes properties in your auth configuration.

Here's how you can set up your auth resource to enable all three sign-in options:

import { defineAuth } from '@aws-amplify/backend';

export const auth = defineAuth({
  loginWith: {
    email: true,
    phone: true,
    username: true
  },
  userAttributes: {
    email: {
      required: true,
      mutable: true
    },
    phoneNumber: {
      required: true,
      mutable: true
    },
    preferredUsername: {
      required: true,
      mutable: true
    }
  }
});

In this configuration:

  1. The loginWith property enables sign-in with email, phone, and username.
  2. The userAttributes property defines these attributes as required during sign-up and allows them to be changed later (mutable).

Note that preferredUsername is used instead of just username as it's one of the standard attributes recognized by Cognito and Amplify.

After making these changes, you'll need to push your updated configuration to apply the changes to your Cognito User Pool. Run amplify push in your terminal to deploy the updates.

This approach allows users to sign up with all three attributes and subsequently use any of them for signing in, providing the flexibility you're looking for in your Amplify Gen2 React application.
Sources
User attributes - AWS Amplify Gen 2 Documentation
Fullstack TypeScript: Reintroducing AWS Amplify | Front-End Web & Mobile
Amplify Gen 2, fullname userAttributes not present on sign up | AWS re:Post

answered 9 months ago
  • I encountered an error while trying your code: "Object literal may only specify known properties, and 'username' does not exist in the specified type." Could you please help me resolve this?

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.