From 6612eb19d1979b1eb7f6dc8b3b6fe160a67fc1d1 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Tue, 2 Jul 2024 05:22:30 -0300 Subject: [PATCH 1/2] fix(realtime): send access token to realtime on initial session --- Sources/Supabase/SupabaseClient.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Supabase/SupabaseClient.swift b/Sources/Supabase/SupabaseClient.swift index a77218fa..75eb81fe 100644 --- a/Sources/Supabase/SupabaseClient.swift +++ b/Sources/Supabase/SupabaseClient.swift @@ -354,7 +354,7 @@ public final class SupabaseClient: Sendable { private func handleTokenChanged(event: AuthChangeEvent, session: Session?) async { let accessToken = mutableState.withValue { - if event == .tokenRefreshed || event == .signedIn, $0.changedAccessToken != session?.accessToken { + if event == .initialSession || event == .tokenRefreshed || event == .signedIn, $0.changedAccessToken != session?.accessToken { $0.changedAccessToken = session?.accessToken return session?.accessToken } else if event == .signedOut { From d7eef0d1a88553d34c5639a2458914f136965b29 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Tue, 2 Jul 2024 05:27:31 -0300 Subject: [PATCH 2/2] refactor --- Sources/Supabase/SupabaseClient.swift | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Sources/Supabase/SupabaseClient.swift b/Sources/Supabase/SupabaseClient.swift index 75eb81fe..f38d9462 100644 --- a/Sources/Supabase/SupabaseClient.swift +++ b/Sources/Supabase/SupabaseClient.swift @@ -353,16 +353,18 @@ public final class SupabaseClient: Sendable { } private func handleTokenChanged(event: AuthChangeEvent, session: Session?) async { - let accessToken = mutableState.withValue { - if event == .initialSession || event == .tokenRefreshed || event == .signedIn, $0.changedAccessToken != session?.accessToken { + let accessToken: String? = mutableState.withValue { + if [.initialSession, .signedIn, .tokenRefreshed].contains(event), $0.changedAccessToken != session?.accessToken { $0.changedAccessToken = session?.accessToken - return session?.accessToken - } else if event == .signedOut { + return session?.accessToken ?? supabaseKey + } + + if event == .signedOut { $0.changedAccessToken = nil return supabaseKey - } else { - return nil } + + return nil } realtime.setAuth(accessToken)