How to set Cognito user pool sign-in options in Serverless?

28 Views Asked by At

I want to set the sign in options of my Cognito user pool to have both User name & Email. So when a user signs up, they will be required to enter an email, a username and a password.

I seem to be able to set this via the AWS console but can't figure out the setting through Serverless or cloud formation (since I think serverless uses this behind the sceens?)

I have the current serverless config which creates a sign host UI sign up form with username and password:

CognitoUserPool:
      Type: 'AWS::Cognito::UserPool'
      Properties:
        UserPoolName: 'MyAppUserPool${self:custom.stage}'
        UsernameConfiguration:
          CaseSensitive: false
        AutoVerifiedAttributes:
          - 'email'
        Policies:
          PasswordPolicy:
            MinimumLength: 8
            RequireUppercase: true
            RequireLowercase: true
            RequireNumbers: true
            RequireSymbols: true
            
    CognitoUserPoolClient:
      Type: 'AWS::Cognito::UserPoolClient'
      Properties:
        ClientName: 'MyAppUserPoolClient${self:custom.stage}'
        GenerateSecret: true
        UserPoolId:
          Ref: 'CognitoUserPool'
        CallbackURLs:
          - 'http://localhost:3000/api/auth/callback/cognito'
        SupportedIdentityProviders:
          - 'COGNITO'
        AllowedOAuthFlowsUserPoolClient: true
        AllowedOAuthFlows: 
          - 'code'
          - 'implicit'
        AllowedOAuthScopes:
          - 'email'
          - 'openid'
          - 'profile'
    
    CognitoUserPoolDomain:
       Type: 'AWS::Cognito::UserPoolDomain'
       Properties:
        Domain: 'my-app-${self:custom.stage}'
        UserPoolId: 
          Ref: 'CognitoUserPool'

I can set the following but this make it just have email and password for the sign up (removes the username:

UsernameAttributes: 
  - 'email'

I am trying to find the config option in the Cloud formation docs here but can't seem to see anything that would allow the config of user pool sign in options.

Any help on how to have a hosted UI with both username and email would be greatly appreciated, thanks.

1

There are 1 best solutions below

0
Eric Z Beard On

I think you will need to add the email as a required attribute on the Schema, instead of trying to add both to the UsernameAttributes.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html#cfn-cognito-userpool-schema

Also, keep in mind that the schema is immutable after creation, so you need to make sure it has everything you want before you start storing data in the user pool.