deleteSession method

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

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,
}) async {
  Log.debug('deleteSession($id, $password, force: $force)', '$runtimeType');

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

  status.value = RxStatus.empty();

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

    return _unauthorized();
  }

  return await WebUtils.protect(() async {
    try {
      FcmRegistrationToken? fcmToken;

      if (PlatformUtils.pushNotifications) {
        final NotificationSettings settings =
            await FirebaseMessaging.instance.getNotificationSettings();

        if (settings.authorizationStatus == AuthorizationStatus.authorized) {
          final String? token = await FirebaseMessaging.instance.getToken(
            vapidKey: Config.vapidKey,
          );

          if (token != null) {
            fcmToken = FcmRegistrationToken(token);
          }
        }
      }

      await _authRepository.deleteSession(token: DeviceToken(fcm: fcmToken));
    } catch (e) {
      printError(info: e.toString());
    }

    return _unauthorized();
  });
}