clearSession method

  1. @override
  2. @visibleForTesting
Future<void> clearSession()

Clears the active session, if any, and removes all derived state.

It is CRITICAL that this function be awaited before returning to the user. Failure to do so will result in very difficult to track down race conditions.

Implementation

@override
@visibleForTesting
Future<void> clearSession() async {
  log.finest('Clearing session');

  if (_tokenRefreshTimer != null) {
    _tokenRefreshTimer!.cancel();
    _tokenRefreshTimer = null;
  }

  // Early exit
  //
  // There could be case when the authenticationState is inProgress and
  // signout is called. For example, if the refresh token has expired. In that
  // case it is important to to clear out the session and remove the refresh
  // token from storage.
  if (authenticationState == AuthenticationState.signedOut) {
    return;
  }

  _session.clear();
  await _authStore.removeItem(refreshTokenClientStorageKey);
  _currentUser = null;

  _loading = false;
  _onTokenChanged();
  _onAuthStateChanged(AuthenticationState.signedOut);
}