Skip to content

Commit 593816b

Browse files
committed
feat: add logging to gotrue
1 parent 8f473f1 commit 593816b

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

packages/gotrue/lib/src/broadcast_web.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import 'dart:html' as html;
33
import 'dart:js_util' as js_util;
44

55
import 'package:gotrue/src/types/types.dart';
6+
import 'package:logging/logging.dart';
67

8+
final _log = Logger('supabase.gotrue');
79
BroadcastChannel getBroadcastChannel(String broadcastKey) {
810
final broadcast = html.BroadcastChannel(broadcastKey);
911
return (
@@ -15,6 +17,7 @@ BroadcastChannel getBroadcastChannel(String broadcastKey) {
1517
return json.decode(json.encode(dataMap));
1618
}),
1719
postMessage: (message) {
20+
_log.fine('Broadcasting message: $message');
1821
final jsMessage = js_util.jsify(message);
1922
broadcast.postMessage(jsMessage);
2023
},

packages/gotrue/lib/src/gotrue_client.dart

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import 'package:jwt_decode/jwt_decode.dart';
1414
import 'package:meta/meta.dart';
1515
import 'package:retry/retry.dart';
1616
import 'package:rxdart/subjects.dart';
17+
import 'package:logging/logging.dart';
1718

1819
import 'broadcast_stub.dart' if (dart.library.html) './broadcast_web.dart'
1920
as web;
@@ -87,6 +88,8 @@ class GoTrueClient {
8788

8889
final AuthFlowType _flowType;
8990

91+
final _log = Logger('supabase.gotrue');
92+
9093
/// Proxy to the web BroadcastChannel API. Should be null on non-web platforms.
9194
BroadcastChannel? _broadcastChannel;
9295

@@ -107,6 +110,9 @@ class GoTrueClient {
107110
_flowType = flowType {
108111
_autoRefreshToken = autoRefreshToken ?? true;
109112

113+
_log.config(
114+
'GoTrueClient initialized with url: $_url, autoRefreshToken: $_autoRefreshToken, flowType: $_flowType, tickDuration: ${Constants.autoRefreshTickDuration}, tickThreshold: ${Constants.autoRefreshTickThreshold}');
115+
110116
final gotrueUrl = url ?? Constants.defaultGotrueUrl;
111117
final gotrueHeader = {
112118
...Constants.defaultHeaders,
@@ -617,6 +623,7 @@ class GoTrueClient {
617623
/// If the current session's refresh token is invalid, an error will be thrown.
618624
Future<AuthResponse> refreshSession([String? refreshToken]) async {
619625
if (currentSession?.accessToken == null) {
626+
_log.warning("Can't refresh session, no current session found.");
620627
throw AuthSessionMissingException();
621628
}
622629

@@ -842,6 +849,7 @@ class GoTrueClient {
842849
Future<void> signOut({
843850
SignOutScope scope = SignOutScope.local,
844851
}) async {
852+
_log.info('Signing out user with scope: $scope');
845853
final accessToken = currentSession?.accessToken;
846854

847855
if (scope != SignOutScope.others) {
@@ -966,13 +974,15 @@ class GoTrueClient {
966974
try {
967975
final session = Session.fromJson(json.decode(jsonStr));
968976
if (session == null) {
977+
_log.warning("Can't recover session from string, session is null");
969978
await signOut();
970979
throw notifyException(
971980
AuthException('Current session is missing data.'),
972981
);
973982
}
974983

975984
if (session.isExpired) {
985+
_log.fine('Session from recovery is expired');
976986
final refreshToken = session.refreshToken;
977987
if (_autoRefreshToken && refreshToken != null) {
978988
return await _callRefreshToken(refreshToken);
@@ -1002,6 +1012,7 @@ class GoTrueClient {
10021012
void startAutoRefresh() async {
10031013
stopAutoRefresh();
10041014

1015+
_log.fine('Starting auto refresh');
10051016
_autoRefreshTicker = Timer.periodic(
10061017
Constants.autoRefreshTickDuration,
10071018
(Timer t) => _autoRefreshTokenTick(),
@@ -1013,6 +1024,7 @@ class GoTrueClient {
10131024

10141025
/// Stops an active auto refresh process running in the background (if any).
10151026
void stopAutoRefresh() {
1027+
_log.fine('Stopping auto refresh');
10161028
_autoRefreshTicker?.cancel();
10171029
_autoRefreshTicker = null;
10181030
}
@@ -1037,12 +1049,15 @@ class GoTrueClient {
10371049
Constants.autoRefreshTickDuration.inMilliseconds)
10381050
.floor();
10391051

1052+
_log.finer('Access token expires in $expiresInTicks ticks');
1053+
10401054
// Only tick if the next tick comes after the retry threshold
10411055
if (expiresInTicks <= Constants.autoRefreshTickThreshold) {
10421056
await _callRefreshToken(refreshToken);
10431057
}
10441058
} catch (error) {
1045-
// Do nothing. JS client prints here
1059+
// Do nothing. JS client prints here, but error is already tracked via
1060+
// [notifyException]
10461061
}
10471062
}
10481063

@@ -1055,6 +1070,7 @@ class GoTrueClient {
10551070
// Make a GET request
10561071
() async {
10571072
attempt++;
1073+
_log.fine('Attempt $attempt to refresh token');
10581074
final options = GotrueRequestOptions(
10591075
headers: _headers,
10601076
body: {'refresh_token': refreshToken},
@@ -1129,11 +1145,13 @@ class GoTrueClient {
11291145

11301146
/// set currentSession and currentUser
11311147
void _saveSession(Session session) {
1148+
_log.fine('Save session: $session');
11321149
_currentSession = session;
11331150
_currentUser = session.user;
11341151
}
11351152

11361153
void _removeSession() {
1154+
_log.fine('Remove session');
11371155
_currentSession = null;
11381156
_currentUser = null;
11391157
}
@@ -1150,6 +1168,7 @@ class GoTrueClient {
11501168
_broadcastChannel = web.getBroadcastChannel(broadcastKey);
11511169
_broadcastChannelSubscription =
11521170
_broadcastChannel?.onMessage.listen((messageEvent) {
1171+
_log.info('Received broadcast message: $messageEvent');
11531172
final rawEvent = messageEvent['event'];
11541173
final event = switch (rawEvent) {
11551174
// This library sends the js name of the event to be comptabile with
@@ -1202,6 +1221,7 @@ class GoTrueClient {
12021221
Future<AuthResponse> _callRefreshToken(String refreshToken) async {
12031222
// Refreshing is already in progress
12041223
if (_refreshTokenCompleter != null) {
1224+
_log.finer("Don't call refresh token, already in progress");
12051225
return _refreshTokenCompleter!.future;
12061226
}
12071227

@@ -1213,6 +1233,7 @@ class GoTrueClient {
12131233
(_) => null,
12141234
onError: (_, __) => null,
12151235
);
1236+
_log.fine('Refresh access token');
12161237

12171238
final data = await _refreshAccessToken(refreshToken);
12181239

@@ -1232,15 +1253,15 @@ class GoTrueClient {
12321253
_removeSession();
12331254
notifyAllSubscribers(AuthChangeEvent.signedOut);
12341255
} else {
1235-
_onAuthStateChangeController.addError(error, stack);
1256+
notifyException(error, stack);
12361257
}
12371258

12381259
_refreshTokenCompleter?.completeError(error);
12391260

12401261
rethrow;
12411262
} catch (error, stack) {
12421263
_refreshTokenCompleter?.completeError(error);
1243-
_onAuthStateChangeController.addError(error, stack);
1264+
notifyException(error, stack);
12441265
rethrow;
12451266
} finally {
12461267
_refreshTokenCompleter = null;
@@ -1265,13 +1286,15 @@ class GoTrueClient {
12651286
});
12661287
}
12671288
final state = AuthState(event, session, fromBroadcast: !broadcast);
1289+
_log.fine('Notifying subscribers: $state');
12681290
_onAuthStateChangeController.add(state);
12691291
_onAuthStateChangeControllerSync.add(state);
12701292
}
12711293

12721294
/// For internal use only.
12731295
@internal
12741296
Object notifyException(Object exception, [StackTrace? stackTrace]) {
1297+
_log.warning('Notifying exception', exception, stackTrace);
12751298
_onAuthStateChangeController.addError(
12761299
exception,
12771300
stackTrace ?? StackTrace.current,

packages/gotrue/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ dependencies:
1616
retry: ^3.1.0
1717
rxdart: '>=0.27.7 <0.29.0'
1818
meta: ^1.7.0
19+
logging: ^1.2.0
1920

2021
dev_dependencies:
2122
dart_jsonwebtoken: ^2.4.1

0 commit comments

Comments
 (0)