@@ -14,6 +14,7 @@ import 'package:jwt_decode/jwt_decode.dart';
14
14
import 'package:meta/meta.dart' ;
15
15
import 'package:retry/retry.dart' ;
16
16
import 'package:rxdart/subjects.dart' ;
17
+ import 'package:logging/logging.dart' ;
17
18
18
19
import 'broadcast_stub.dart' if (dart.library.html) './broadcast_web.dart'
19
20
as web;
@@ -87,6 +88,8 @@ class GoTrueClient {
87
88
88
89
final AuthFlowType _flowType;
89
90
91
+ final _log = Logger ('supabase.gotrue' );
92
+
90
93
/// Proxy to the web BroadcastChannel API. Should be null on non-web platforms.
91
94
BroadcastChannel ? _broadcastChannel;
92
95
@@ -107,6 +110,9 @@ class GoTrueClient {
107
110
_flowType = flowType {
108
111
_autoRefreshToken = autoRefreshToken ?? true ;
109
112
113
+ _log.config (
114
+ 'GoTrueClient initialized with url: $_url , autoRefreshToken: $_autoRefreshToken , flowType: $_flowType , tickDuration: ${Constants .autoRefreshTickDuration }, tickThreshold: ${Constants .autoRefreshTickThreshold }' );
115
+
110
116
final gotrueUrl = url ?? Constants .defaultGotrueUrl;
111
117
final gotrueHeader = {
112
118
...Constants .defaultHeaders,
@@ -617,6 +623,7 @@ class GoTrueClient {
617
623
/// If the current session's refresh token is invalid, an error will be thrown.
618
624
Future <AuthResponse > refreshSession ([String ? refreshToken]) async {
619
625
if (currentSession? .accessToken == null ) {
626
+ _log.warning ("Can't refresh session, no current session found." );
620
627
throw AuthSessionMissingException ();
621
628
}
622
629
@@ -842,6 +849,7 @@ class GoTrueClient {
842
849
Future <void > signOut ({
843
850
SignOutScope scope = SignOutScope .local,
844
851
}) async {
852
+ _log.info ('Signing out user with scope: $scope ' );
845
853
final accessToken = currentSession? .accessToken;
846
854
847
855
if (scope != SignOutScope .others) {
@@ -966,13 +974,15 @@ class GoTrueClient {
966
974
try {
967
975
final session = Session .fromJson (json.decode (jsonStr));
968
976
if (session == null ) {
977
+ _log.warning ("Can't recover session from string, session is null" );
969
978
await signOut ();
970
979
throw notifyException (
971
980
AuthException ('Current session is missing data.' ),
972
981
);
973
982
}
974
983
975
984
if (session.isExpired) {
985
+ _log.fine ('Session from recovery is expired' );
976
986
final refreshToken = session.refreshToken;
977
987
if (_autoRefreshToken && refreshToken != null ) {
978
988
return await _callRefreshToken (refreshToken);
@@ -1002,6 +1012,7 @@ class GoTrueClient {
1002
1012
void startAutoRefresh () async {
1003
1013
stopAutoRefresh ();
1004
1014
1015
+ _log.fine ('Starting auto refresh' );
1005
1016
_autoRefreshTicker = Timer .periodic (
1006
1017
Constants .autoRefreshTickDuration,
1007
1018
(Timer t) => _autoRefreshTokenTick (),
@@ -1013,6 +1024,7 @@ class GoTrueClient {
1013
1024
1014
1025
/// Stops an active auto refresh process running in the background (if any).
1015
1026
void stopAutoRefresh () {
1027
+ _log.fine ('Stopping auto refresh' );
1016
1028
_autoRefreshTicker? .cancel ();
1017
1029
_autoRefreshTicker = null ;
1018
1030
}
@@ -1037,12 +1049,15 @@ class GoTrueClient {
1037
1049
Constants .autoRefreshTickDuration.inMilliseconds)
1038
1050
.floor ();
1039
1051
1052
+ _log.finer ('Access token expires in $expiresInTicks ticks' );
1053
+
1040
1054
// Only tick if the next tick comes after the retry threshold
1041
1055
if (expiresInTicks <= Constants .autoRefreshTickThreshold) {
1042
1056
await _callRefreshToken (refreshToken);
1043
1057
}
1044
1058
} catch (error) {
1045
- // Do nothing. JS client prints here
1059
+ // Do nothing. JS client prints here, but error is already tracked via
1060
+ // [notifyException]
1046
1061
}
1047
1062
}
1048
1063
@@ -1055,6 +1070,7 @@ class GoTrueClient {
1055
1070
// Make a GET request
1056
1071
() async {
1057
1072
attempt++ ;
1073
+ _log.fine ('Attempt $attempt to refresh token' );
1058
1074
final options = GotrueRequestOptions (
1059
1075
headers: _headers,
1060
1076
body: {'refresh_token' : refreshToken},
@@ -1129,11 +1145,13 @@ class GoTrueClient {
1129
1145
1130
1146
/// set currentSession and currentUser
1131
1147
void _saveSession (Session session) {
1148
+ _log.fine ('Save session: $session ' );
1132
1149
_currentSession = session;
1133
1150
_currentUser = session.user;
1134
1151
}
1135
1152
1136
1153
void _removeSession () {
1154
+ _log.fine ('Remove session' );
1137
1155
_currentSession = null ;
1138
1156
_currentUser = null ;
1139
1157
}
@@ -1150,6 +1168,7 @@ class GoTrueClient {
1150
1168
_broadcastChannel = web.getBroadcastChannel (broadcastKey);
1151
1169
_broadcastChannelSubscription =
1152
1170
_broadcastChannel? .onMessage.listen ((messageEvent) {
1171
+ _log.info ('Received broadcast message: $messageEvent ' );
1153
1172
final rawEvent = messageEvent['event' ];
1154
1173
final event = switch (rawEvent) {
1155
1174
// This library sends the js name of the event to be comptabile with
@@ -1202,6 +1221,7 @@ class GoTrueClient {
1202
1221
Future <AuthResponse > _callRefreshToken (String refreshToken) async {
1203
1222
// Refreshing is already in progress
1204
1223
if (_refreshTokenCompleter != null ) {
1224
+ _log.finer ("Don't call refresh token, already in progress" );
1205
1225
return _refreshTokenCompleter! .future;
1206
1226
}
1207
1227
@@ -1213,6 +1233,7 @@ class GoTrueClient {
1213
1233
(_) => null ,
1214
1234
onError: (_, __) => null ,
1215
1235
);
1236
+ _log.fine ('Refresh access token' );
1216
1237
1217
1238
final data = await _refreshAccessToken (refreshToken);
1218
1239
@@ -1232,15 +1253,15 @@ class GoTrueClient {
1232
1253
_removeSession ();
1233
1254
notifyAllSubscribers (AuthChangeEvent .signedOut);
1234
1255
} else {
1235
- _onAuthStateChangeController. addError (error, stack);
1256
+ notifyException (error, stack);
1236
1257
}
1237
1258
1238
1259
_refreshTokenCompleter? .completeError (error);
1239
1260
1240
1261
rethrow ;
1241
1262
} catch (error, stack) {
1242
1263
_refreshTokenCompleter? .completeError (error);
1243
- _onAuthStateChangeController. addError (error, stack);
1264
+ notifyException (error, stack);
1244
1265
rethrow ;
1245
1266
} finally {
1246
1267
_refreshTokenCompleter = null ;
@@ -1265,13 +1286,15 @@ class GoTrueClient {
1265
1286
});
1266
1287
}
1267
1288
final state = AuthState (event, session, fromBroadcast: ! broadcast);
1289
+ _log.fine ('Notifying subscribers: $state ' );
1268
1290
_onAuthStateChangeController.add (state);
1269
1291
_onAuthStateChangeControllerSync.add (state);
1270
1292
}
1271
1293
1272
1294
/// For internal use only.
1273
1295
@internal
1274
1296
Object notifyException (Object exception, [StackTrace ? stackTrace]) {
1297
+ _log.warning ('Notifying exception' , exception, stackTrace);
1275
1298
_onAuthStateChangeController.addError (
1276
1299
exception,
1277
1300
stackTrace ?? StackTrace .current,
0 commit comments