TheQ
TheQ

Reputation: 2019

How to sign out of aws cognito - android?

So here is the code i used to sign my user into cognito (i hope im correct). Now, how would i sign out? Currently i have my own signing up process (so no facebook or google yet).

 // Callback handler for the sign-in process
    private AuthenticationHandler authenticationHandler = new AuthenticationHandler()
    {
        @Override
        public void onSuccess(CognitoUserSession userSession, CognitoDevice newDevice)
        {
            Log.d(COGNITO_LOGIN,"Login success!");
            cognitoUser.getDetailsInBackground(getDetailsHandler);
            //Now we get user from dynamoDB and store it into a local user object. 
        }
        @Override
        public void getAuthenticationDetails(AuthenticationContinuation authenticationContinuation, String userId)
        {
            Log.d(COGNITO_LOGIN,passwordET.getText().toString());
            // The API needs user sign-in credentials to continue
            AuthenticationDetails authenticationDetails = new AuthenticationDetails(userId, passwordET.getText().toString(), 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("verificationCode");
            // 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
            Log.d(COGNITO_LOGIN,"Login failed!");
            Log.d(COGNITO_LOGIN,exception.getMessage());
        }
    };
 cognitoUser.getSessionInBackground(authenticationHandler);

Upvotes: 5

Views: 5544

Answers (3)

tomdiz
tomdiz

Reputation: 1

CognitoUserPool pool = AwsCognitoHelper.getPool();
if (pool != null) {
    CognitoUser user = pool.getCurrentUser();
    if (user != null) {
        GenericHandler handler = new GenericHandler() {

            @Override
            public void onSuccess() {
            }

            @Override
            public void onFailure(Exception e) {
            }
        };
        user.globalSignOutInBackground(handler);
    }
}

Upvotes: 0

Ionut Trestian
Ionut Trestian

Reputation: 5751

You should be able to just call signOut on a cognitoUser object such as below. What that does is clear access, id and refresh tokens from the device so you would need to authenticate again.

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

There is also a globalSignOut call that revokes tokens server-side.

Upvotes: 2

shobhan
shobhan

Reputation: 252

There is a way to wipe or clear the session for the current user who logged, the following is the way, which I found so far.

This is for fb in federated identities 
  if (fbAccessToken != null) {
                                    LoginManager.getInstance().logOut();
                                }
This is for twiiter 
                                if (mAuthManager != null) {
                                      mAuthManager.clearAuthorizationState(null);
                                }

                                // wipe data
                                CognitoSyncClientManager.getInstance()
                                        .wipeData();

Upvotes: 0

Related Questions