signOut method

Future<void> signOut(
  1. BuildContext context, {
  2. bool redirectToLogin = false,
})

Signs out the current user and clears all stored tokens and Cognito session

redirectToLogin - If true, automatically redirects to login page after logout

Implementation

Future<void> signOut(BuildContext context, {bool redirectToLogin = false}) async {
  try {
    // Clear local tokens and cancel refresh timer
    await TokenStorage.clearTokens();
    _refreshTimer?.cancel();

    // Clear Cognito session via WebView
    await _clearCognitoSession(context, redirectToLogin: redirectToLogin);

    // // Redirect to login page if requested
    // if (redirectToLogin) {
    //   await signIn(context);
    // }
  } catch (e) {
    // Even if WebView logout fails, we've cleared local tokens
    // Log the error but don't throw to ensure local cleanup is done
    debugPrint('Error during Cognito logout: $e');

    // Still attempt redirect if requested, even if logout had errors
    if (redirectToLogin) {
      await signIn(context);
    }
  }
}