deleteExpiredSessions method

Future<void> deleteExpiredSessions(
  1. Session session, {
  2. bool deleteExpired = true,
  3. bool deleteInactive = true,
  4. Transaction? transaction,
})

Deletes the session where AuthSession.expiresAt has elapsed, or where the session has not been used in since AuthSession.expireAfterUnusedFor expired it.

Implementation

Future<void> deleteExpiredSessions(
  final Session session, {
  /// Whether to delete sessions which [AuthSession.expiresAt] is in the past.
  final bool deleteExpired = true,

  /// Whether to delete sessions which have not been used for [AuthSession.expireAfterUnusedFor].
  final bool deleteInactive = true,
  final Transaction? transaction,
}) async {
  if (deleteExpired) {
    await AuthSession.db.deleteWhere(
      session,
      where: (final t) => t.expiresAt < clock.now(),
      transaction: transaction,
    );
  }

  if (deleteInactive) {
    await session.db.unsafeQuery(
      'DELETE FROM ${AuthSession.t.tableName} WHERE '
      '"${AuthSession.t.expireAfterUnusedFor.columnName}" IS NOT NULL AND '
      '"${AuthSession.t.lastUsedAt.columnName}" + ("${AuthSession.t.expireAfterUnusedFor.columnName}" * INTERVAL \'1 millisecond\') < \'${SerializationManager.encode(clock.now())}\'',
      transaction: transaction,
    );
  }
}