4.2.1 Using user pools with AWS Mobile SDK

Here are the details about registering, confirming, and authenticating users using standard AWS Mobile SDK.

Creating an AWSCognitoIdentityUserPool Object

The following procedure describes how to create an AWSCognitoIdentityUserPool object to interact with.

// Create a user pool with default ClientConfiguration
CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret, cognitoRegion);

OR

// This will also work
ClientConfiguration clientConfiguration = new ClientConfiguration();
AmazonCognitoIdentityProvider cipClient = new AmazonCognitoIdentityProviderClient(new AnonymousAWSCredentials(), clientConfiguration);
cipClient.setRegion(Region.getRegion(cognitoRegion));
CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret, cipClient);

Register a User

Use userPool.signUpInBackground method to sign up a user.

SignUpHandler signupCallback = new SignUpHandler() {

    @Override
    public void onSuccess(CognitoUser cognitoUser, SignUpResult signUpResult) {
        // Sign-up was successful

        // Check if this user (cognitoUser) needs to be confirmed
        if(!signUpResult.getUserConfirmed()) {
            // This user must be confirmed and a confirmation code was sent to the user
            // cognitoUserCodeDeliveryDetails will indicate where the confirmation code was sent
            // Get the confirmation code from user
        }
        else {
            // The user has already been confirmed
        }
    }

    @Override
    public void onFailure(Exception exception) {
        // Sign-up failed, check exception for the cause
    }
};

// API call
userPool.signUpInBackground(userId, password, userAttributes, null, signupCallback);

Confirm Signup

Confirm a user's sign up with the confirmation code using user.confirmSignUp method

GenericHandler handler = new GenericHandler() {

    @Override
    public void onSuccess() {
        // User was successfully confirmed!
    }
    @Override
    public void onFailure(Exception exception) {
        // Confirmation failed, probe exception for details
    }
}

// API call
user.confirmSignUp(code, handler);

Sign in a User

Use cognitoUser.getSessionInBackground method to get a session with the username and password.

// Callback handler for the sign-in process 
AuthenticationHandler authenticationHandler = new AuthenticationHandler() { 

    @Override
    public void onSuccess(CognitoUserSession cognitoUserSession, CognitoDevice device) { 
        // Sign-in was successful, cognitoUserSession will contain tokens for the user   
    }

    @Override
    public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId) { 
        // The API needs user sign-in credentials to continue
        AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, password, null);

        // Pass the user sign-in credentials to the continuation
        authenticationContinuation.setAuthenticationDetails(authenticationDetails);

        // Allow the sign-in to continue
        authenticationContinuation.continueTask();
    }

    @Override
    public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) { 
        // Multi-factor authentication is required; get the verification code from user
        multiFactorAuthenticationContinuation.setMfaCode(mfaVerificationCode);
        // Allow the sign-in process to continue
        multiFactorAuthenticationContinuation.continueTask();
    }
    
    @Override
    public void authenticationChallenge(ChallengeContinuation continuation) {
           }

    @Override
    public void onFailure(Exception exception) {
        // Sign-in failed, check exception for the cause
    } 
};

// Sign in the user 
cognitoUser.getSessionInBackground(authenticationHandler);

Sign out a user

Use cognitoUser.signOut method to log a user out.

// This has cleared all tokens and this user will have to go through the authentication process to get tokens.
user.signOut();

Last updated