From 048e81b9ca5a317ad4340c4bae60f556d9e31584 Mon Sep 17 00:00:00 2001 From: Guilherme Souza Date: Tue, 2 Jul 2024 08:06:15 -0300 Subject: [PATCH] fix(realtime): send access token to realtime on initial session (#439) --- 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 a77218fa..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 == .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)