From 7e660c3cac6ab40923131aba6804c3a9921d7b9d Mon Sep 17 00:00:00 2001 From: dshukertjr Date: Thu, 12 Dec 2024 14:08:59 +0900 Subject: [PATCH] fix: properly handle exception on supabase-client for realtime set auth --- .../realtime_client/lib/src/realtime_client.dart | 3 ++- packages/supabase/lib/src/supabase_client.dart | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/realtime_client/lib/src/realtime_client.dart b/packages/realtime_client/lib/src/realtime_client.dart index 67b5de66..ce4904ad 100644 --- a/packages/realtime_client/lib/src/realtime_client.dart +++ b/packages/realtime_client/lib/src/realtime_client.dart @@ -429,7 +429,8 @@ class RealtimeClient { null, Level.FINE, ); - throw 'InvalidJWTToken: Invalid value for JWT claim "exp" with value ${parsed['exp']}'; + throw FormatException( + 'InvalidJWTToken: Invalid value for JWT claim "exp" with value ${parsed['exp']}'); } } } diff --git a/packages/supabase/lib/src/supabase_client.dart b/packages/supabase/lib/src/supabase_client.dart index 4f500b8b..fe592ff0 100644 --- a/packages/supabase/lib/src/supabase_client.dart +++ b/packages/supabase/lib/src/supabase_client.dart @@ -350,7 +350,17 @@ class SupabaseClient { // ignore: invalid_use_of_internal_member _authStateSubscription = auth.onAuthStateChangeSync.listen( (data) { - _handleTokenChanged(data.event, data.session?.accessToken); + try { + _handleTokenChanged(data.event, data.session?.accessToken); + } on FormatException catch (e) { + if (e.message.contains('InvalidJWTToken')) { + // The exception is thrown by RealtimeClient when the token is + // expired for example on app launch after the app has been closed + // for a while. + } else { + rethrow; + } + } }, onError: (error, stack) {}, );