Menu
Amazon Cognito
Developer Guide (Version Last Updated: 07/28/2016)

Examples of Using User Pools with the Mobile SDK for Android

This topic provides code examples that perform basic tasks using the Mobile SDK for Android. Since the SDK makes network calls, all API calls should be made from a non-activity thread.

Create a CognitoUserPool

Copy
CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret); // user pool can also be created with client app configuration: CognitoUserPool userPool = new CognitoUserPool(context, userPoolId, clientId, clientSecret, clientConfiguration);

Register a New User

Copy
// create a handler for registration SignUpHandler handler = new SignUpHandler() { @Override public void onSuccess(CognitoUser user, CognitoUserCodeDeliveryDetails codeDeliveryDetails) { // If the sign up was successful, "user" is a CognitoUser object of the user who was signed up. // "codeDeliveryDetails" will contain details about where the confirmation codes will be delivered. } @Override public void onFailure(Exception exception) { // Sign up failed, code check the exception for cause and perform remedial actions. } }

Get the Cached User

Copy
CognitoUser user = userPool.getCurrentUser();

Create a User Object with a UserId

Copy
CognitoUser user = userPool.getUser(userId);

Confirm a User

Copy
// create a callback handler for confirm GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // User was successfully confirmed! } @Override public void onFailure(Exception exception) { // Confirmation failed, probe exception for details } } user.confirmSignUp(code, handler);

Request a Confirmation Code

Copy
// create a callback handler for the confirmation code request GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Confirmation code was successfully sent! } @Override public void onFailure(Exception exception) { // Confirmation code request failed, probe exception for details } } user.resendConfirmationCode(handler);

Forgot Password: Get Code to Set New Password

Copy
ForgotPasswordHandler handler = new ForgotPasswordHandler() { @Override public void onSuccess() { // Forgot password process completed successfully, new password has been successfully set } @Override public void getResetCode(ForgotPasswordContinuation continuation) { // A code will be sent, use the "continuation" object to continue with the forgot password process // This will indicate where the code was sent String codeSentHere = continuation.getParameters(); // Code to get the code from the user - user dialogs etc. // If the program control has to exit this method, take the "continuation" object. // "continuation" is the only possible way to continue with the process // When the code is available // Set the new password continuation.setPassword(newPassword); // Set the code to verify continuation.setVerificationCode(code); // Let the forgot password process proceed continuation.continueTask(); } /** * This is called for all fatal errors encountered during the password reset process * Probe {@exception} for cause of this failure. * @param exception */ public void onFailure(Exception exception) { // Forgot password processing failed, probe the exception for cause } } user.forgotPassword(handler);

Authentication Handler: Get Tokens

Copy
// Implement authentication handler, AuthenticationHandler handler = new AuthenticationHandler() { @Override public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice) { // Authentication was successful, the "userSession" will have the current valid tokens // Time to do awesome stuff } @Override public void getAuthenticationDetails(final AuthenticationContinuation continuation, final String userID) { // User authentication details, userId and password are required to continue. // Use the "continuation" object to pass the user authentication details // After the user authentication details are available, wrap them in an AuthenticationDetails class // Along with userId and password, parameters for user pools for Lambda can be passed here // The validation parameters "validationParameters" are passed in as a Map<String, String> AuthenticationDetails authDetails = new AuthenticationDetails(userId, password, validationParameters); // Now allow the authentication to continue continuation.setAuthenticationDetails(authDetails); continuation.continueTask(); } @Override public void getMFACode(final MultiFactorAuthenticationContinuation continuation) { // Multi-factor authentication is required to authenticate // A code was sent to the user, use the code to continue with the authentication // Find where the code was sent to String codeSentHere = continuation.getParameter()[0]; // When the verification code is available, continue to authenticate continuation.setMfaCode(code); continuation.continueTask(); } @Override public void authenticationChallenge(final ChallengeContinuation continuation) { // A custom challenge has to be solved to authenticate // Set the challenge responses // Call continueTask() method to respond to the challenge and continue with authentication. } @Override public void onFailure(final Exception exception) { // Authentication failed, probe exception for the cause } }; user.getSession(handler);

Get User Details

Copy
GetDetailsHandler handler = new GetDetailsHandler() { @Override public void onSuccess(final CognitoUserDetails list) { // Successfully retrieved user details } @Override public void onFailure(final Exception exception) { // Failed to retrieve the user details, probe exception for the cause } }; user.getDetails(handler);

Get Attribute Verification Code

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Attribute verification code was successfully sent! } @Override public void onFailure(Exception exception) { // Attribute verification code request failed, probe exception for details } }; user.getAttibuteVerificationCode(attributeName, handler);

Verify Attribute

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Attribute verification was successful! } @Override public void onFailure(Exception exception) { // Attribute verification failed, probe exception for details } }; user.verifyAttribute(attributeName, code, handler);

Delete Attribute

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Attribute deletion was successful! } @Override public void onFailure(Exception exception) { // Attribute deletion failed, probe exception for details } }; user.deleteAttribute(attributeName, handler);

Change Password

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Password change was successful! } @Override public void onFailure(Exception exception) { // Password change failed, probe exception for details } }; user.changePassword(oldPassword, newPassword, handler);

Change or Set User Settings

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Successfully changed settings! } @Override public void onFailure(Exception exception) { // Change failed, probe exception for details } }; // userSettings is an object of the type CognitoUserSettings, CognitoUserSettings userSettings = new CognitoUserSettings(); // Set the user settings userSettings.setSettings(settingName, settingValue); // Now update the new settings to the Amazon Cognito Identity Provider Service user.setUserSettings(userSettings, handler);

Delete User

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Delete was successful! } @Override public void onFailure(Exception exception) { // Delete failed, probe exception for details } }; user.deleteUser(handler);

Sign Out User

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

Get Access and ID Tokens from CognitoUserSession

Copy
// Session is an object of the type CognitoUserSession String accessToken = session.getAccessToken().getJWT(); String idToken = session.getIdToken().getJWTToken();

List All Devices for a User

Copy
DevicesHandler devicesHandler = new DevicesHandler() { @Override public void onSuccess(List<CognitoDevice> devices) { // devices will contain a list of all remembered devices } @Override public void onFailure(Exception e) { // List devices failed, probe exception for details } }; user.listDevicesInBackground(10, null, devicesHandler);

Remember a Device

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Successful! } @Override public void onFailure(Exception exception) { // Failed, probe exception for details } }; cognitoDevice.rememberThisDeviceInBackground(handler)

Do Not Remember a Device

Copy
GenericHandler handler = new GenericHandler() { @Override public void onSuccess() { // Successful! } @Override public void onFailure(Exception exception) { // Failed, probe exception for details } }; cognitoDevice.doNotRememberThisDeviceInBackground(handler)