11import  'dart:async' ;
22
33import  'package:http/http.dart' ;
4+ import  'package:logging/logging.dart' ;
45import  'package:supabase/src/constants.dart' ;
6+ import  'package:supabase/src/version.dart' ;
57import  'package:supabase/supabase.dart' ;
68import  'package:yet_another_json_isolate/yet_another_json_isolate.dart' ;
79
@@ -66,6 +68,8 @@ class SupabaseClient {
6668  /// Increment ID of the stream to create different realtime topic for each stream 
6769   final  _incrementId =  Counter ();
6870
71+   final  _log =  Logger ('supabase.supabase' );
72+ 
6973  /// Getter for the HTTP headers 
7074   Map <String , String > get  headers {
7175    return  _headers;
@@ -145,7 +149,12 @@ class SupabaseClient {
145149    storage =  _initStorageClient (storageOptions.retryAttempts);
146150    realtime =  _initRealtimeClient (options:  realtimeClientOptions);
147151    if  (accessToken ==  null ) {
152+       _log.config (
153+           'Initialize SupabaseClient v$version  with no custom access token' );
148154      _listenForAuthEvents ();
155+     } else  {
156+       _log.config (
157+           'Initialize SupabaseClient v$version  with custom access token' );
149158    }
150159  }
151160
@@ -223,6 +232,8 @@ class SupabaseClient {
223232    return  realtime.removeAllChannels ();
224233  }
225234
235+   /// Get either the custom access token from [accessToken]  or the supabase one 
236+   /// from [_authInstance]  
226237   Future <String ?> _getAccessToken () async  {
227238    if  (accessToken !=  null ) {
228239      return  await  accessToken !();
@@ -231,14 +242,19 @@ class SupabaseClient {
231242    if  (_authInstance.currentSession? .isExpired ??  false ) {
232243      try  {
233244        await  _authInstance.refreshSession ();
234-       } catch  (error) {
245+       } catch  (error, stackTrace ) {
235246        final  expiresAt =  _authInstance.currentSession? .expiresAt;
236247        if  (expiresAt !=  null ) {
237248          // Failed to refresh the token. 
238249          final  isExpiredWithoutMargin =  DateTime .now ()
239250              .isAfter (DateTime .fromMillisecondsSinceEpoch (expiresAt *  1000 ));
240251          if  (isExpiredWithoutMargin) {
241252            // Throw the error instead of making an API request with an expired token. 
253+             _log.warning (
254+               'Access token is expired and refreshing failed, aborting api request' ,
255+               error,
256+               stackTrace,
257+             );
242258            rethrow ;
243259          }
244260        }
@@ -248,6 +264,7 @@ class SupabaseClient {
248264  }
249265
250266  Future <void > dispose () async  {
267+     _log.fine ('Dispose SupabaseClient' );
251268    await  _authStateSubscription? .cancel ();
252269    await  _isolate.dispose ();
253270    auth.dispose ();
0 commit comments