resolveAuthSessionForStrategyWithCallbacks<TContext> function

Future<AuthSessionResolution> resolveAuthSessionForStrategyWithCallbacks<TContext>({
  1. required AuthSessionStrategy strategy,
  2. required AuthCallbacks<TContext> callbacks,
  3. required TContext context,
  4. required JwtSessionOptions jwtOptions,
  5. required Duration? sessionUpdateAge,
  6. AuthPrincipal? readSessionPrincipal()?,
  7. void applySessionMaxAge()?,
  8. String? readSessionIssuedAt()?,
  9. void writeSessionIssuedAt(
    1. DateTime issuedAtUtc
    )?,
  10. void touchSession()?,
  11. DateTime? resolveSessionExpiry()?,
  12. String? readJwtToken()?,
  13. FutureOr<bool> validateJwtClaims(
    1. Map<String, dynamic> claims,
    2. AuthUser user
    )?,
  14. void writeJwtAttribute(
    1. String key,
    2. Object? value
    )?,
  15. Map<String, dynamic>? protectedJwtClaims,
  16. Client? httpClient,
  17. DateTime? now,
})

Resolves resolveSession behavior for an auth session strategy.

The session branch returns an empty resolution when no principal is read; otherwise it applies max-age and refresh hooks. The JWT branch verifies and optionally refreshes the token, writes payload attributes, and returns a refresh cookie when one was issued. Callback and token errors propagate.

Session strategy uses callback hooks for framework-specific principal/session IO and session refresh touch semantics.

JWT strategy verifies and optionally refreshes token claims through resolveAuthJwtClaimsWithCallbacks.

Implementation

Future<AuthSessionResolution>
resolveAuthSessionForStrategyWithCallbacks<TContext>({
  required AuthSessionStrategy strategy,
  required AuthCallbacks<TContext> callbacks,
  required TContext context,
  required JwtSessionOptions jwtOptions,
  required Duration? sessionUpdateAge,
  AuthPrincipal? Function()? readSessionPrincipal,
  void Function()? applySessionMaxAge,
  String? Function()? readSessionIssuedAt,
  void Function(DateTime issuedAtUtc)? writeSessionIssuedAt,
  void Function()? touchSession,
  DateTime? Function()? resolveSessionExpiry,
  String? Function()? readJwtToken,
  FutureOr<bool> Function(Map<String, dynamic> claims, AuthUser user)?
  validateJwtClaims,
  void Function(String key, Object? value)? writeJwtAttribute,
  Map<String, dynamic>? protectedJwtClaims,
  http.Client? httpClient,
  DateTime? now,
}) async {
  switch (strategy) {
    case AuthSessionStrategy.session:
      final principal = readSessionPrincipal?.call();
      if (principal == null) {
        return const AuthSessionResolution();
      }

      applySessionMaxAge?.call();
      syncAuthSessionRefresh(
        issuedAtValue: readSessionIssuedAt?.call(),
        updateAge: sessionUpdateAge,
        now: now,
        writeIssuedAt: (issuedAtUtc) => writeSessionIssuedAt?.call(issuedAtUtc),
        touchSession: touchSession,
      );
      return AuthSessionResolution(
        session: AuthSession(
          user: AuthUser.fromPrincipal(principal),
          expiresAt: resolveSessionExpiry?.call(),
          strategy: AuthSessionStrategy.session,
        ),
      );
    case AuthSessionStrategy.jwt:
      final resolved = await resolveAuthJwtSessionWithRefresh(
        token: readJwtToken?.call(),
        options: jwtOptions,
        updateAge: sessionUpdateAge,
        httpClient: httpClient,
        now: now,
        validateClaims: validateJwtClaims,
        resolveClaims: (claims, user) =>
            resolveAuthJwtClaimsWithCallbacks<TContext>(
              callbacks: callbacks,
              context: context,
              user: user,
              strategy: AuthSessionStrategy.jwt,
              token: claims,
              protectedClaims: protectedJwtClaims,
            ),
      );
      if (resolved == null) {
        return const AuthSessionResolution();
      }
      final writeAttribute = writeJwtAttribute;
      if (writeAttribute != null) {
        writeJwtPayloadAttributes(
          resolved.payload,
          setAttribute: writeAttribute,
        );
      }
      return AuthSessionResolution(
        session: AuthSession(
          user: resolved.user,
          expiresAt: resolved.expiresAt,
          strategy: AuthSessionStrategy.jwt,
          token: resolved.token,
        ),
        refreshCookie: resolved.refreshCookie,
      );
  }
}