AWS Mobile
Developer Guide

Sign Out a Signed-in User

Prerequisite

This section describes how to add sign-out flow for a signed-in user. In the following example, the built-in AWS Mobile SDK sign-in/sign-up screen is displayed after the user signs out.

The examples on this page assume that you have added the AWS Mobile SDK to your mobile app using the steps on the Add User Sign-in page, and have configured an identity provider.

Enable User Sign-out

Android - JavaAndroid - KotliniOS - Swift
Android - Java

In the following example, AWSMobileClient is instantiated within the onCreate method of an activity called AuthenticatorActivity. If the client does not find a cached identity from a previous sign-in, it retrieves an unauthenticated “guest” Amazon Cognito Federated Identity ID that is used to access other AWS services. In Logcat, look for the string: Welcome to AWS! to see that the client has successfully instantiated.

If the user already has a cached authenticated identity ID from a previous sign-in, then AWSMobileClient will resume the session without an additional sign-in.

A SignInStateChangeListener object is added to IdentityManager, which captures onUserSignedIn and onUserSignedOut events.

Finally, showSignIn() is called to create a SignInUI object, and to call the object's login method. This displays the built-in sign-in UI of the SDK, and defines MainActivity as the navigation target of a successful sign-in. The SignInUI calls are placed in a separate function so they can also easily be called when the onUserSignedOut event fires.

In Logcat, a successful sign-in prints the string; Sign-in succeeded.

// AuthenticatorActivity.java package com.YOUR-DOMAIN.android.YOUR-APP-NAME; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; // AWSMobileClient imports import com.amazonaws.mobile.client.AWSMobileClient; import com.amazonaws.mobile.client.AWSStartupHandler; import com.amazonaws.mobile.client.AWSStartupResult; // AWS SDK sign-in UI imports import com.amazonaws.mobile.auth.core.IdentityHandler; import com.amazonaws.mobile.auth.core.IdentityManager; import com.amazonaws.mobile.auth.core.SignInStateChangeListener; import com.amazonaws.mobile.auth.ui.SignInUI; public class AuthenticatorActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_authenticator); AWSMobileClient.getInstance().initialize(this).execute(); // Sign-in listener IdentityManager.getDefaultIdentityManager().addSignInStateChangeListener(new SignInStateChangeListener() { @Override public void onUserSignedIn() { Log.d(LOG_TAG, "User Signed In"); } // Sign-out listener @Override public void onUserSignedOut() { Log.d(LOG_TAG, "User Signed Out"); showSignIn(); } }); showSignIn(); } /* * Display the AWS SDK sign-in/sign-up UI */ private void showSignIn() { Log.d(LOG_TAG, "showSignIn"); SignInUI signin = (SignInUI) AWSMobileClient.getInstance().getClient(AuthenticatorActivity.this, SignInUI.class); signin.login(AuthenticatorActivity.this, MainActivity.class).execute(); } }

MainActivity displays a sign-out button, that calls the signOut() method of the IdentityManager. This will fire the SignInStateChangeListener.onSignedOut() event defined in the AuthenticatorActivity. In Logcat, you should see the string: Signing out....

onUserSignedOut() then calls showSignIn which causes the sign-in screen to reappear.

package com.dzmedia.android.YOUR-APP-NAME; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.amazonaws.mobile.auth.core.IdentityHandler; import com.amazonaws.mobile.auth.core.IdentityManager; import com.amazonaws.mobile.client.AWSMobileClient; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Create log out Button on click listener Button clickButton = (Button) findViewById(R.id.signOutButton); clickButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { IdentityManager.getDefaultIdentityManager().signOut(); } }); } // other MainActivity code . . . }
Android - Kotlin

In the following example, AWSMobileClient is instantiated within the onCreate method of an activity called AuthenticatorActivity. If the client does not find a cached identity from a previous sign-in, it retrieves an unauthenticated “guest” Amazon Cognito Federated Identity ID that is used to access other AWS services. In Logcat, look for the string: Welcome to AWS! to see that the client has successfully instantiated.

If the user already has a cached authenticated identity ID from a previous sign-in, then AWSMobileClient will resume the session without an additional sign-in.

A SignInStateChangeListener object is added to IdentityManager, which captures onUserSignedIn and onUserSignedOut events.

Finally, showSignIn() is called to create a SignInUI object, and to call the object's login method. This displays the built-in sign-in UI of the SDK, and defines MainActivity as the navigation target of a successful sign-in. The SignInUI calls are placed in a separate function so they can also easily be called when the onUserSignedOut event fires.

In Logcat, a successful sign-in prints the string; Sign-in succeeded.

// AuthenticatorActivity.java package com.your-domain.android.YOUR-APP-NAME; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.TextView; // AWSMobileClient imports import com.amazonaws.mobile.client.AWSMobileClient; import com.amazonaws.mobile.client.AWSStartupHandler; import com.amazonaws.mobile.client.AWSStartupResult; // AWS SDK sign-in UI imports import com.amazonaws.mobile.auth.core.IdentityHandler; import com.amazonaws.mobile.auth.core.IdentityManager; import com.amazonaws.mobile.auth.core.SignInStateChangeListener; import com.amazonaws.mobile.auth.ui.SignInUI; class AuthenticatorActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) AWSMobileClient.getInstance().initialize(this).execute() // Sign-in listener IdentityManager.defaultIdentityManager.addSignInStateChangeListener( object : SignInStateChangeListener() { override fun onUserSignedIn() { // Do something } override fun onUserSignedOut() { showSignIn() } } ) showSignIn() } private fun showSignIn() { AWSMobileClient.getInstance().initialize(this) { val ui = AWSMobileClient.getInstance().getClient( this@AuthenticatorActivity, SignInUI::class.java) as SignInUI? ui?.login( this@AuthenticatorActivity, MainActivity::class.java)?.execute() }.execute() } }

MainActivity displays a sign-out button, that calls the signOut() method of the IdentityManager. This will fire the SignInStateChangeListener.onSignedOut() event defined in the AuthenticatorActivity. In Logcat, you should see the string: Signing out....

onUserSignedOut() then calls showSignIn which causes the sign-in screen to reappear.

package com.YOUR-DOMAIN.android.YOUR-APP-NAME; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.amazonaws.mobile.auth.core.IdentityHandler; import com.amazonaws.mobile.auth.core.IdentityManager; import com.amazonaws.mobile.client.AWSMobileClient; class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) signOutButton.onClick { IdentityManager.defaultIdentityManager.signOut() } } }
iOS - Swift

In the following example, AWSMobileClient is instantiated within the didfinishlaunching and open url blocks in AppDelegate, as described in Add User Sign-In. If the client does not find a cached identity from a previous sign-in, it retrieves an unauthenticated “guest” Amazon Cognito Federated Identity ID that is used to access other AWS services. In debug output, look for the string: Welcome to AWS!.

If the user already has a cached authenticated identity ID from a previous sign-in, then AWSMobileClient will resume the session without an additional sign-in.

When AWSMobileClient is instantiated, the app navigates to a Navigation Control hosted in a ViewController whose UIView contains a sign-out button. If the user is not already signed in, the viewDidLoad of the ViewController calls the built-in sign-in UI of the AWS Mobile SDK. A successful sign-in prints the string: Sign-in succeeded to debug output.

In the action of the sign-out button, a successful sign-out calls for the sign-in screen to be displayed again.

// ViewController.swift import UIKit import AWSMobileClient import AWSAuthCore import AWSAuthCore import AWSAuthUI class ViewController: UIViewController { @IBOutlet weak var textfield: UITextField! public var identityId: String = "" override func viewDidLoad() { super.viewDidLoad() showSignIn() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func signOutButtonPress(_ sender: Any) { AWSSignInManager.sharedInstance().logout(completionHandler: {(result: Any?, error: Error?) in self.showSignIn() // print("Sign-out Successful: \(signInProvider.getDisplayName)"); }) } func showSignIn() { AWSAuthUIViewController.presentViewController(with: self.navigationController!, configuration: nil, completionHandler: { (provider: AWSSignInProvider, error: Error?) in if error != nil { print("Error occurred: \(String(describing: error))") } else { print("Sign-in successful.") } }) } }

On this page: