signOut method

Future<SignOutResult> signOut({
  1. SignOutOptions? options,
})

Sign the user out of the current device.

Optionally accepts plugin options which allow customizing provider-specific behavior, e.g. the Cognito User Pool.

For more information, see the Amplify docs.

Examples

import 'package:amplify_auth_cognito/amplify_auth_cognito.dart';
import 'package:amplify_flutter/amplify_flutter.dart';
Future<void> signOutCurrentUser() async {
  final result = await Amplify.Auth.signOut();
  if (result is CognitoCompleteSignOut) {
    safePrint('Sign out completed successfully');
  } else if (result is CognitoFailedSignOut) {
    safePrint('Error signing user out: ${result.exception.message}');
  }
}

You can pass options for the request, which can be used for global sign out as well as other plugin-specific options.

In the case of the Cognito plugin, passing globalSignOut: true to the API can result in instances of partial sign out, where the user's session is cleared locally, but an error occurred performing global sign out. In this case, the exception is returned along with the token which can be used to manually retry the global sign out if desired.

Future<void> signOutGlobally() async {
  final result = await Amplify.Auth.signOut(
    options: const SignOutOptions(globalSignOut: true),
  );
  if (result is CognitoCompleteSignOut) {
    safePrint('Sign out completed successfully');
  } else if (result is CognitoPartialSignOut) {
    final globalSignOutException = result.globalSignOutException!;
    final accessToken = globalSignOutException.accessToken;
    // Retry the global sign out using the access token, if desired
    // ...
    safePrint('Error signing user out: ${globalSignOutException.message}');
  } else if (result is CognitoFailedSignOut) {
    safePrint('Error signing user out: ${result.exception.message}');
  }
}

Implementation

Future<SignOutResult> signOut({
  SignOutOptions? options,
}) =>
    identifyCall(
      AuthCategoryMethod.signOut,
      () => defaultPlugin.signOut(options: options),
    );