Skip to content

Commit 7fbb305

Browse files
committed
Export ability to get session without validation
1 parent e5c564a commit 7fbb305

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Sources/Auth/AuthClient.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ public final class AuthClient: Sendable {
2727
}
2828
}
2929

30+
/// Returns the session.
31+
///
32+
/// - Parameters:
33+
/// - shouldValidateExpiration: If the session should be refresh, if necessary.
34+
///
35+
/// If no session can be found, a ``AuthError/sessionNotFound`` error is thrown.
36+
public func session(shouldValidateExpiration: Bool = true) async throws -> Session {
37+
try await sessionManager.session(shouldValidateExpiration: shouldValidateExpiration)
38+
}
39+
3040
/// Namespace for accessing multi-factor authentication API.
3141
public let mfa = AuthMFA()
3242
/// Namespace for the GoTrue admin methods.

Sources/Auth/Internal/SessionManager.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ actor SessionManager {
1717
}
1818

1919
func session(shouldValidateExpiration: Bool = true) async throws -> Session {
20+
guard let currentSession = try storage.getSession() else {
21+
throw AuthError.sessionNotFound
22+
}
23+
24+
25+
if currentSession.isValid || !shouldValidateExpiration {
26+
return currentSession.session
27+
}
28+
2029
if let task {
2130
return try await task.value
2231
}
2332

2433
task = Task {
2534
defer { task = nil }
2635

27-
guard let currentSession = try storage.getSession() else {
28-
throw AuthError.sessionNotFound
29-
}
30-
31-
if currentSession.isValid || !shouldValidateExpiration {
32-
return currentSession.session
33-
}
34-
3536
let session = try await sessionRefresher.refreshSession(currentSession.session.refreshToken)
3637
try update(session)
3738
return session

0 commit comments

Comments
 (0)