How to check Email Already exists in AWS Cognito?

纵饮孤独 提交于 2020-12-30 08:26:05

问题


I am using AWS Cognito for signin/signup. We have two step from.

1) It will ask for email. 2) If email already exists then it will ask Password or otherwise it will say create password. Button on this step displayed based on the above condition either Login or Register.

Here after user enters email, I need a way to check in cognito with AWS javascript SDK to check email already registered or not.

Thanks,


回答1:


Amazon Amplify makes the signIn and signUp process very straightforward with the Auth importation from aws-amplify in Angular/React. On my login page I ask every user to sign up whether or not their email is alrealdy stored in the user pool. If a user is registered, Cognito raises a "UserExistsException" exception which can be catch in the Auth.signUp promise like so :

public cognitoSignUp(username, password, email){ Auth.signUp({ username, password, attributes: { email,
}, validationData: [] }) .then(data => { console.log(data) }) .catch(error => { //The user has already registered so go to the SignIn method if(error['code'] === "UsernameExistsException"){ this.cognitoSignIn(username, password); } else{ console.log(error) } }); }

Hope that my answer was useful.




回答2:


  1. If you want to check if user exists on Front-end with aws amplify, i found the answer here https://github.com/aws-amplify/amplify-js/issues/1067

    userExist(email: string) {
    return this.cognitoService.userExist(email.toLowerCase()).then(res => {
        return false;
    }).catch(error => {
        const code = error.code;
        console.log(error);
        switch (code) {
            case 'UserNotFoundException':
                return !this.redirectToRegister(email);
            case 'NotAuthorizedException':
                return true;
            case 'PasswordResetRequiredException':
              return !this.forgotPassword(email);
            case 'UserNotConfirmedException':
                return !this.redirectToCompleteRegister(email);
            default:
                return false;
        }
      });
    }
    
  2. If you want to check on server side with nodeJS if the user is free and doesn't already exist

    checkIfUserDoesntExist(email) {
    return new Promise(async (resolve, reject) => {
        const payload = {
            ClientId: configCognito.APP_CLIENT_ID,
            AuthFlow: "ADMIN_NO_SRP_AUTH",
            UserPoolId: configCognito.USER_POOL_ID,
            AuthParameters: {
                USERNAME: email,
                PASSWORD: "123",
            }
        }
        try {
            await this.cognito.adminInitiateAuth(payload).promise();
            reject(); // very unlikely 
        } catch (e) {
            console.log("checkIfUserDoesntExist error", e);
            switch (e.code) {
                case 'UserNotFoundException':
                    resolve();
                case 'NotAuthorizedException':
                case 'PasswordResetRequiredException':
                case 'UserNotConfirmedException':
                default:
                    reject();
            }
        }
      });
    }
    


来源:https://stackoverflow.com/questions/51083100/how-to-check-email-already-exists-in-aws-cognito

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!