4.2.2 Using user pools with Bayun AWSS3 wrapper 'SecureAuthentication'

User Registration, SignUp Confirmation, SignIn, SignOut needs to be done with SecureAuthentication instance.

Set up your service config

There is no change in setting up Service Config and is same as using standard AWS Mobile SDK.

// 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);

Set Up the SecureAuthentication object

The SecureAuthentication is a singleton object, and must be provided with context, appId and companyName before using it. This will serve as the object on which all function calls are to be made.

SecureAuthentication secureAuthentication = SecureAuthentication.getInstance();

secureAuthentication.setContext(appContext);
secureAuthentication.setAppId(APP_ID);
secureAuthentication.setAppSecret(APP_SECRET);
secureAuthentication.setApplicationKeySalt(APP_SALT);
secureAuthentication.setCompanyName(companyName);

Register a User

Use SecureAuthentication's method signUp to register a new user instead of relying on standard AWS Mobile SDK's signUp method.

boolean registerBayunWithPwd = true;

// Hashmap to save the signup fields
HashMap signUpFields = new HashMap<String, String>();

// Read user data and register
CognitoUserAttributes userAttributes = new CognitoUserAttributes();
userAttributes.addAttribute(signUpFields.put("Email", "email@mydomain.com")

// SignupHandler to handle signup outcomes.
SignUpHandler signUpHandler = new SignUpHandler() {
    @Override
    public void onSuccess(CognitoUser user, SignUpResult signUpResult) {
        if (signUpResult.getUserConfirmed()) {
            // User is already confirmed
            // handle the case where user identity is already confirmed.
        }
        else {
            // User is not confirmed
            // handle the case where user has to confirm his identity
        }
    }

    @Override
    public void onFailure(Exception exception) {
        // Handle failure.
    }
};

// Signup call
SecureAuthentication.getInstance().signUp(activityContext, userPool, usernameInput, userpasswordInput, userAttributes, null, signUpHandler, registerBayunWithPwd);

Confirm Signup

Confirm a users' sign up with the confirmation code using SecureAuthentication's confirmSignUp method. Use this method instead of CognitoUser's method, to confirm signup with both Cognito and Bayun.

// Callback to handle the confirmation api call.
GenericHandler confHandler = new GenericHandler() {
    @Override
    public void onSuccess() {
        Log.d(TAG, "User confirmed.");
        // Handle success.
    }

    @Override
    public void onFailure(Exception exception) {
        // Handle failure.
    }
};

// Call to confirm the user.
SecureAuthentication.getInstance().confirmSignUp(activityContext, cognitoUser, confirmCode, forcedAliasCreation, confHandler);

Sign in a user

Use SecureAuthentication's signIn method to get a session, using username and password, with both Cognito and Bayun, instead of CognitoUser's method.

// Callback to handle the signIn api call.
AuthenticationHandler authenticationHandler = new AuthenticationHandler() {
    @Override
    public void onSuccess(CognitoUserSession cognitoUserSession, CognitoDevice device) {
    Log.d(TAG, "User sign in success.");
        // Handle success.
        // This block is also executed when a user is already signed in.
    }

    @Override
    public void getAuthenticationDetails(AuthenticationContinuation continuation, String username) {
        AuthenticationDetails authenticationDetails = new AuthenticationDetails(username, password, validationData);
        continuation.setAuthenticationDetails(authenticationDetails);
        continuation.continueTask();
    }

    @Override
    public void getMFACode(MultiFactorAuthenticationContinuation multiFactorAuthenticationContinuation) {
        // Handle this block, if needed.
    }

    @Override
    public void onFailure(Exception e) {
        // Handle failure.
    }

    @Override
    public void authenticationChallenge(ChallengeContinuation continuation) {
        /**
         * For Custom authentication challenge, implement your logic to present challenge to the
         * user and pass the user's responses to the continuation.
         */
    }
};

// Call to sign in a user.
SecureAuthentication.getInstance().signIn(activityContext, username, password, cognitoUser, authenticationHandler);

Sign out a user

Use SecureAuthentication's signOut method to clear all tokens and logout of Bayun as well, instead of using CognitoUser's method. User will have to go through the authentication process to get tokens.

SecureAuthentication.getInstance().signOut(cognitoUser);

Last updated