简体   繁体   中英

How to implement signOut once signed in with Cognito/UserPool on Android

Looking for some help on implementing the signOut() function to allow the user to sign out of the app.

I have implemented the code found here: https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-user-sign-in.html

I was able to get the AWS sign-in screen working and i can see the new user created in Cognito as well, however when I try implemented a sign-out button to take me back to the previous intent, it keeps bouncing back. I did some reading around and found that user will always stay signed in.

Below is the code I have for the AuthenticatorActivity:

import com.amazonaws.mobile.auth.ui.SignInUI;
import com.amazonaws.mobile.client.AWSMobileClient;
import com.amazonaws.mobile.client.AWSStartupHandler;
import com.amazonaws.mobile.client.AWSStartupResult;


public class AuthenticatorActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authenticator);

        // Add a call to initialize AWSMobileClient
        AWSMobileClient.getInstance().initialize(this, new AWSStartupHandler() {
            @Override
            public void onComplete(AWSStartupResult awsStartupResult) {
                SignInUI signin = (SignInUI) AWSMobileClient.getInstance().getClient(AuthenticatorActivity.this, SignInUI.class);
                signin.login(AuthenticatorActivity.this, MainActivity.class).execute();
            }
        }).execute();
    }
}

Once user enters in details to sign up and then signs in, the next activity/intent displayed also has the below sign-out button visible.

Below code is what I have when the sign out button is pressed (in another Activity):

/** called when the user taps the send button */
    public void signOutOfApp(View view) {
        Intent intent = new Intent(this, AuthenticatorActivity.class);
        startActivity(intent);
    }

Could someone please help me out in suggesting how I could get this to work? The AuthenticatorActivity doesn't appear to have anything to indicate where it is passing/storing the new user details and when i get to the next activity/intent, I am not sure how i can call/get the user details to pass that to the signout method. Please help guide me here! Thanks in advance!

The signOut method is available through the IdentityManager class. The following will let you invoke signOut. Since signOut is asynchronous, you may want to register a callback with the IdentityManager in order to get a callback when the user has signed out successfully.

Register the callback for getting notified of the sign-in state change:

IdentityManager.getDefaultIdentityManager().addSignInStateChangeListener(new SignInStateChangeListener() {
                @Override
                public void onUserSignedIn() {
                    Log.d(LOG_TAG, "User Signed In");
                }

                @Override
                public void onUserSignedOut() {
                    Log.d(LOG_TAG, "User Signed Out");
                }
});

To invoke signOut:

IdentityManager.getDefaultIdentityManager().signOut();

How does AWSMobileClient interact with the IdentityManager ?

AWSMobileClient creates the IdentityManager object and set the object as the default IdentityManager . Since IdentityManager is a singleton, you can use IdentityManager.getDefaultIdentityManager() anywhere from your app.

as for Kotlin since I can login using AWS, where do I add this in my nextActivity and the signout also IdentityManager.getDefaultIdentityManager().signOut();

IdentityManager.getDefaultIdentityManager().addSignInStateChangeListener(
    object : SignInStateChangeListener() {
        override fun onUserSignedIn() {
            Log.d(TAG, "User signed in");
        }

        override fun onUserSignedOut() {
            Log.d(TAG, "User signed out");
        }
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM