AWS-Cognito: How to sign in user with email or username

Viewed 3767

During AWS Cogito User Pool there are two options for how to let users sign in. One of them is

Username - Users can use a username and optionally multiple alternatives to sign up and sign in.

Also allow sign in with verified email address

I have selected the above and while testing it using aws-amplify I always get __type: "NotAuthorizedException" message: "Incorrect username or password." if I try to sign in with email but it works fine when I try to sign in with the username.

Here is my signin method

Auth.signIn(email, password)
  .then(r => {
    setLoggedIn(true);
  })
  .catch(r => {
    setLoggedIn(false);
  })

};

3 Answers

I know this is an old thread, but I've recently had this problem and had to figure it out. When you have a user in your user pool that does not have the email_verified set to True, the Auth.signIn method will only work with that user's username.

Not only that, but the Auth.forgotPassword method won't send the code to the user's e-mail. There's possibly more interactions like these.

Make sure you confirm your user's email, that's all!

If you are using a backend to create your users, you can simply do this by:

const signUpParams = {
  UserPoolId: "YOUR_USER_POOL_ID",
  TemporaryPassword: "TEMP_PW",
  Username: "username",
  UserAttributes: [
    {
      Name: 'email',
      Value: "USER_EMAIL",
    },
    {
      Name: 'email_verified',
      Value: 'true',
    },
  ],
};

const { User } = await cognitoService.adminCreateUser(signUpParams).promise();

aws cognito is case sensitive example if you registered your email with Siyavash@email.com then you cannot sign in with siyavash@email.com

My vuex store action for login: You may need to set the attribute name to username

async login({ commit },{ email, password }) {
    const user =  await Auth.signIn({ username: email, password });
    commit('set', user)
    return user;
  },
Related