Skip to content

Commit

Permalink
pull token from third party or fallback to auth
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Dec 4, 2024
1 parent 22d82e9 commit 6264e7e
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions Sources/Supabase/SupabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,7 @@ public final class SupabaseClient: Sendable {
}

private func adapt(request: URLRequest) async -> URLRequest {
let token: String? =
if let accessToken = options.auth.accessToken {
try? await accessToken()
} else {
try? await auth.session.accessToken
}
let token = try? await _getAccessToken()

var request = request
if let token {
Expand All @@ -362,6 +357,14 @@ public final class SupabaseClient: Sendable {
return request
}

private func _getAccessToken() async throws -> String {
if let accessToken = options.auth.accessToken {
try await accessToken()
} else {
try await auth.session.accessToken
}
}

private func listenForAuthEvents() {
let task = Task {
for await (event, session) in auth.authStateChanges {
Expand Down Expand Up @@ -404,8 +407,7 @@ public final class SupabaseClient: Sendable {

if realtimeOptions.accessToken == nil {
realtimeOptions.accessToken = { [weak self] in
guard let self else { return "" }
return try await self.auth.session.accessToken
try await self?._getAccessToken() ?? ""
}
} else {
reportIssue(
Expand Down

0 comments on commit 6264e7e

Please sign in to comment.