pruneTokens method

Future pruneTokens(
  1. dynamic resourceOwnerIdentifier
)

Deletes expired tokens for resourceOwnerIdentifier.

If the resource owner identified by resourceOwnerIdentifier has expired tokens and they have reached their token issuance limit, this method will delete tokens until they are in compliance with that limit.

There is rarely a need to invoke this method directly, as it is invoked each time a new token is issued.

Implementation

Future pruneTokens(dynamic resourceOwnerIdentifier) async {
  final oldTokenQuery = Query<ManagedAuthToken>(context!)
    ..where((o) => o.resourceOwner).identifiedBy(resourceOwnerIdentifier)
    ..sortBy((t) => t.expirationDate, QuerySortOrder.descending)
    ..offset = tokenLimit
    ..fetchLimit = 1
    ..returningProperties((t) => [t.expirationDate]);

  final results = await oldTokenQuery.fetch();
  if (results.length == 1) {
    final deleteQ = Query<ManagedAuthToken>(context!)
      ..where((o) => o.resourceOwner).identifiedBy(resourceOwnerIdentifier)
      ..where((o) => o.expirationDate)
          .lessThanEqualTo(results.first.expirationDate);

    return deleteQ.delete();
  }
}