How to verify if a user with a given email already exists in User Pool?

See original GitHub issue

Do you want to request a feature or report a bug?

Question

What is the expected behavior?

Check is user exists

example

Is there such a method?

Auth.verifyIfUserExists({
    email: 'foo@bar.com'
})
.then(res => {
    // user with this email already exists
})

Because now I can find out if a user exists ONLY during signUp action. But I want to check it before I do signUp. Because if a user doesn’t exist it will be created right off the bat. And it is not what expected

Auth.signUp({
        username,
        password,
        attributes: {
        },
    })
    .then(data => console.log(data))
    .catch(err =>{
        // User exists !!
    });

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:24
  • Comments:39 (2 by maintainers)

github_iconTop GitHub Comments

46reactions
heri16commented, Nov 7, 2018

@wzup

There is not function that does this and only this; however, I think if you use the confirmSignUp function and are using email as an alias you will get back an AliasExistsException error.

In any case I am marking this as a feature request, as it seems useful.

Thanks for your feedback.

I looked at the amplify source code.

Auth.confirmSignup() calls cognitoUser.confirmRegistration(code, forceAliasCreation) which then calls this API: https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmSignUp.html

The right way to do this (without signIn & signOut) according to the project contributors seems to be this:

const code = '000000'
Auth.confirmSignUp(username, code, {
    // If set to False, the API will throw an AliasExistsException error if the phone number/email used already exists as an alias with a different user
    forceAliasCreation: false
}).then(data => console.log(data))
  .catch( err => {
        switch ( err.code ) {
            case 'UserNotFoundException':
                return true;
            case 'NotAuthorizedException':
                return false;
            case 'AliasExistsException':
                // Email alias already exists
                return false;
            case 'CodeMismatchException':
                return false;
            case 'ExpiredCodeException':
                return false;
            default:
                return false;
        }
    } )
20reactions
tcchaucommented, Jul 5, 2018

@wzup The sign-in workaround might work just because User Pools require passwords that are 6-characters are longer so in practice, there will never be a user account whose password is ‘123’.

@haverchuck However, even if this workaround works, it’s really bad that the API doesn’t support checking the existence of a user name directly. I’ve been working with Cognito for two years and this feature already exists as a request, but hasn’t been implemented yet, along with the ability for an administrator to reset an account’s password. The combination of these two problems makes it quite difficult to build enterprise applications.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check Email Already exists in AWS Cognito?
1) It will ask for email. 2) If email already exists then it will ask Password or otherwise it will say create password....
Read more >
Signing up and confirming user accounts - Amazon Cognito
When you verify your users' email addresses and phone numbers, you ensure that you can contact your users. Complete the following steps in...
Read more >
Cognito/Amplify - How to check if user exists? : r/aws - Reddit
User pool app clients have a setting "PreventUserExistenceErrors" that controls whether actions like ForgetPassword throw an error if the ...
Read more >
check if user exists in cognito
In order to check if an email already exists in Cognito we have to call .signIn. // Because there is no special method...
Read more >
Pre sign-up Lambda trigger - Amazon Cognito
You can set autoVerifyEmail to true to auto-verify the user's email. ... If an alias with that email address already exists, the alias...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found