deleteSession method

Future<String?> deleteSession({
  1. SessionId? id,
  2. UserPassword? password,
  3. bool force = false,
  4. bool keepData = true,
})

Deletes Session with the provided id, if any, or otherwise Session of the active MyUser.

Returns the path of the authentication page.

If force is true, then the current Credentials will be revoked unilaterally and immediately.

Implementation

Future<String?> deleteSession({
  SessionId? id,
  UserPassword? password,
  bool force = false,
  bool keepData = true,
}) async {
  Log.debug(
    'deleteSession($id, $password, force: $force, keepData: $keepData)',
    '$runtimeType',
  );

  if (id != null) {
    await _authRepository.deleteSession(id: id, password: password);
    return null;
  }

  status.value = RxStatus.empty();

  final Future<void> Function({bool keepData})? logoutCallback = onLogout;
  Future<void> handleCallbacks() async {
    if (logoutCallback != null) {
      Log.debug('Invoking `onLogout(keepData: $keepData)`', '$runtimeType');

      try {
        await logoutCallback(keepData: keepData);
      } catch (e) {
        Log.debug('Unable to invoke `onLogout()`: $e', '$runtimeType');
      }

      onLogout = null;
      hasCalls = null;
    }
  }

  if (force) {
    if (userId != null) {
      _authRepository.removeAccount(userId!);
    }

    _unauthorized();

    await handleCallbacks();

    return Routes.auth;
  }

  return await WebUtils.protect(() async {
    try {
      await _authRepository.deleteSession();
    } catch (e) {
      printError(info: e.toString());
    }

    _unauthorized();

    await handleCallbacks();

    return Routes.auth;
  });
}