Aws amplify gen 2 nextjs Cognito not allowing signin with email | phone and password

0

I'm creating an aws amplify gen 2 nextjs app, where I have a usecase, I want the user to signup using either email or phone. Having said that, I have enabled both email and phone number for cognito user pool. I tried to signup with phone number, it is working fine. However, later I added my email, got it verified via code, it does not allow me to login with email and password now. I tried to add the email under preferred_username as well, still there is no luck. I'm not sure what exactly I'm missing

1 Answer
0
It appears you're encountering an issue with sign-in options in your AWS Amplify Gen 2 Next.js application using Amazon Cognito. The problem you're describing is likely related to how the login attributes are configured in your Amplify Gen 2 auth setup.

In Amplify Gen 2, the auth configuration is typically defined in the `resource.ts` file. To allow users to sign in with both email and phone number, you need to properly configure the `loginWith` and `userAttributes` properties in your auth resource definition.

Here's how you can set up your auth resource to enable both email and phone sign-in options:

1. In your `resource.ts` file, ensure your auth configuration includes:

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

2. This configuration enables sign-in with both email and phone, and sets both attributes as required during sign-up and mutable (can be changed later).

3. After making these changes, you'll need to push your updated configuration by running `amplify push` in your terminal to apply the changes to your Cognito User Pool.

It's important to note that Amplify Gen 2 currently doesn't support using a username as an alias attribute for sign-in. The sign-in options are limited to email and phone number.

If you've already created users in your user pool, you may need to ensure that both email and phone number are verified for existing users. Users should be able to sign in with whichever attribute (email or phone) they used during sign-up, as long as it's verified.

If you're still experiencing issues after making these changes, you may want to check your Cognito User Pool settings directly in the AWS Console to ensure they align with your Amplify configuration. Also, make sure you're using the latest version of Amplify libraries in your project.
  

Sources
How can I add all three alias attributes in AWS Cognito while setting up Amplify Gen2 Auth? | AWS re:Post
Sign-up - AWS Amplify Gen 2 Documentation

profile picture
answered 24 days ago
  • export const auth = defineAuth({ loginWith: { email: { verificationEmailStyle: 'CODE', }, phone: true, externalProviders: { google: { clientId: secret('GOOGLE_CLIENT_ID'), clientSecret: secret('GOOGLE_CLIENT_SECRET'), scopes: ['email', 'profile'], }, signInWithApple: { clientId: secret('APPLE_CLIENT_ID'), keyId: secret('APPLE_KEY_ID'), privateKey: secret('APPLE_PRIVATE_KEY'), teamId: secret('APPLE_TEAM_ID'), }, callbackUrls: [ 'http://localhost:3000/', ], logoutUrls: [ 'http://localhost:3000/', ], }, }, senders: { email: { fromEmail: process.env.NEXT_PUBLIC_SENDER_EMAIL '', fromName: process.env.NEXT_PUBLIC_SENDER_NAME '', }, }, userAttributes: { email: { required: false, }, phoneNumber: { required: false, }, }, multifactor: { mode: 'OPTIONAL', sms: true, }, triggers: { postConfirmation, customMessage, }, });

    I have already done this, its not working for me

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.

Guidelines for Answering Questions