fetchAuthSession method

Future<AuthSession> fetchAuthSession({
  1. FetchAuthSessionOptions? options,
})

Fetch the current auth session.

Optionally accepts plugin options which allow customizing provider-specific behavior.

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> fetchAuthSession() async {
  try {
    final result = await Amplify.Auth.fetchAuthSession();
    safePrint('User is signed in: ${result.isSignedIn}');
  } on AuthException catch (e) {
    safePrint('Error retrieving auth session: ${e.message}');
  }
}

Sometimes it can be helpful to retrieve the instance of the underlying plugin which has more specific typing. In the case of Cognito, calling fetchAuthSession on the Cognito plugin returns AWS-specific values such as the identity ID, AWS credentials, and Cognito User Pool tokens.

Future<void> fetchCognitoAuthSession() async {
  try {
    final cognitoPlugin = Amplify.Auth.getPlugin(AmplifyAuthCognito.pluginKey);
    final result = await cognitoPlugin.fetchAuthSession();
    final identityId = result.identityIdResult.value;
    safePrint("Current user's identity ID: $identityId");
  } on AuthException catch (e) {
    safePrint('Error retrieving auth session: ${e.message}');
  }
}

Implementation

Future<AuthSession> fetchAuthSession({
  FetchAuthSessionOptions? options,
}) =>
    identifyCall(
      AuthCategoryMethod.fetchAuthSession,
      () => defaultPlugin.fetchAuthSession(options: options),
    );