Skip to content

Commit

Permalink
fix: allow removing session on broadcast and add bool to AuthState
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinzent03 committed Sep 10, 2024
1 parent 40c29e1 commit 8cb8735
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/gotrue/lib/src/gotrue_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1173,9 +1173,12 @@ class GoTrueClient {
session = Session.fromJson(messageEvent['session']);
} catch (e) {
// ignore
return;
}
if (session != null) {
_saveSession(session);
} else {
_removeSession();
}
notifyAllSubscribers(event, session: session, broadcast: false);
}
Expand Down Expand Up @@ -1244,19 +1247,23 @@ class GoTrueClient {
}

/// For internal use only.
///
/// [broadcast] is used to determine if the event should be broadcasted to
/// other tabs.
@internal
void notifyAllSubscribers(
AuthChangeEvent event, {
Session? session,
bool broadcast = true,
}) {
session ??= currentSession;
if (broadcast && event != AuthChangeEvent.initialSession) {
_broadcastChannel?.postMessage({
'event': event.jsName,
'session': session?.toJson(),
});
}
final state = AuthState(event, session ?? currentSession);
final state = AuthState(event, session, fromBroadcast: !broadcast);
_onAuthStateChangeController.add(state);
_onAuthStateChangeControllerSync.add(state);
}
Expand Down
8 changes: 7 additions & 1 deletion packages/gotrue/lib/src/types/auth_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import 'package:gotrue/src/types/session.dart';
class AuthState {
final AuthChangeEvent event;
final Session? session;
final bool fromBroadcast;

AuthState(this.event, this.session);
AuthState(this.event, this.session, {this.fromBroadcast = false});

@override
String toString() {
return 'AuthState{event: $event, session: $session, fromBroadcast: $fromBroadcast}';
}
}

0 comments on commit 8cb8735

Please sign in to comment.