checkAccountStatus method

Future<void> checkAccountStatus(
  1. Session session, {
  2. required void onExpiredUserAuthentication(
    1. UuidValue authUserId
    ),
  3. Transaction? transaction,
  4. int databaseBatchSize = 100,
})

Checks whether all accounts are in good standing with Apple and that the authorization has not been revoked.

Accounts are checked at most every 24 hours.

In case a deactivated account is encountered, the configuration's expiredUserAuthenticationCallback is invoked with the auth user's ID. Then all their sessions created through Sign in with Apple should be revoked.

Implementation

Future<void> checkAccountStatus(
  final Session session, {
  /// Callback to be invoked when an Apple authentication has been revoked.
  ///
  /// In this case all sessions associated with this sign-in method should be
  /// removed.
  required final void Function(UuidValue authUserId)
      onExpiredUserAuthentication,
  final Transaction? transaction,
  final int databaseBatchSize = 100,
}) async {
  while (true) {
    final appleAccounts = await AppleAccount.db.find(
      session,
      where: (final t) =>
          t.lastRefreshedAt <
          DateTime.now().subtract(const Duration(days: 1)),
      limit: databaseBatchSize,
      transaction: transaction,
    );

    if (appleAccounts.isEmpty) {
      break;
    }

    for (final appleAccount in appleAccounts) {
      try {
        await _siwa.validateRefreshToken(
          appleAccount.refreshToken,
          useBundleIdentifier:
              appleAccount.refreshTokenRequestedWithBundleIdentifier,
        );
      } on RevokedTokenException catch (_) {
        onExpiredUserAuthentication(appleAccount.authUserId);
      }

      await AppleAccount.db.updateRow(
        session,
        appleAccount.copyWith(lastRefreshedAt: clock.now()),
      );
    }
  }
}