Skip to content

Commit

Permalink
Export ability to get session without validation
Browse files Browse the repository at this point in the history
  • Loading branch information
leoMehlig committed May 9, 2024
1 parent e5c564a commit 7fbb305
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 10 additions & 0 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public final class AuthClient: Sendable {
}
}

/// Returns the session.
///
/// - Parameters:
/// - shouldValidateExpiration: If the session should be refresh, if necessary.
///
/// If no session can be found, a ``AuthError/sessionNotFound`` error is thrown.
public func session(shouldValidateExpiration: Bool = true) async throws -> Session {
try await sessionManager.session(shouldValidateExpiration: shouldValidateExpiration)
}

/// Namespace for accessing multi-factor authentication API.
public let mfa = AuthMFA()
/// Namespace for the GoTrue admin methods.
Expand Down
17 changes: 9 additions & 8 deletions Sources/Auth/Internal/SessionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ actor SessionManager {
}

func session(shouldValidateExpiration: Bool = true) async throws -> Session {
guard let currentSession = try storage.getSession() else {
throw AuthError.sessionNotFound
}


if currentSession.isValid || !shouldValidateExpiration {
return currentSession.session
}

if let task {
return try await task.value
}

task = Task {
defer { task = nil }

guard let currentSession = try storage.getSession() else {
throw AuthError.sessionNotFound
}

if currentSession.isValid || !shouldValidateExpiration {
return currentSession.session
}

let session = try await sessionRefresher.refreshSession(currentSession.session.refreshToken)
try update(session)
return session
Expand Down

0 comments on commit 7fbb305

Please sign in to comment.