Skip to content

Commit 9b323b9

Browse files
committed
fix: handle realtime token exception on SupabaseClient
1 parent 7e660c3 commit 9b323b9

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

packages/supabase/lib/src/supabase_client.dart

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -349,32 +349,32 @@ class SupabaseClient {
349349
void _listenForAuthEvents() {
350350
// ignore: invalid_use_of_internal_member
351351
_authStateSubscription = auth.onAuthStateChangeSync.listen(
352-
(data) {
353-
try {
354-
_handleTokenChanged(data.event, data.session?.accessToken);
355-
} on FormatException catch (e) {
356-
if (e.message.contains('InvalidJWTToken')) {
357-
// The exception is thrown by RealtimeClient when the token is
358-
// expired for example on app launch after the app has been closed
359-
// for a while.
360-
} else {
361-
rethrow;
362-
}
363-
}
352+
(data) async {
353+
await _handleTokenChanged(data.event, data.session?.accessToken);
364354
},
365355
onError: (error, stack) {},
366356
);
367357
}
368358

369-
void _handleTokenChanged(AuthChangeEvent event, String? token) {
359+
Future<void> _handleTokenChanged(AuthChangeEvent event, String? token) async {
370360
if (event == AuthChangeEvent.initialSession ||
371361
event == AuthChangeEvent.tokenRefreshed ||
372362
event == AuthChangeEvent.signedIn) {
373-
realtime.setAuth(token);
363+
try {
364+
await realtime.setAuth(token);
365+
} on FormatException catch (e) {
366+
if (e.message.contains('InvalidJWTToken')) {
367+
// The exception is thrown by RealtimeClient when the token is
368+
// expired for example on app launch after the app has been closed
369+
// for a while.
370+
} else {
371+
rethrow;
372+
}
373+
}
374374
} else if (event == AuthChangeEvent.signedOut) {
375375
// Token is removed
376376

377-
realtime.setAuth(_supabaseKey);
377+
await realtime.setAuth(_supabaseKey);
378378
}
379379
}
380380
}

0 commit comments

Comments
 (0)