Skip to content

fix(auth, web): ensure exact same streams are not unsubscribed #13033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions packages/firebase_auth/firebase_auth_web/lib/src/interop/auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'dart:async';
import 'dart:js_interop';
import 'package:firebase_auth_platform_interface/firebase_auth_platform_interface.dart';
import 'package:firebase_core_web/firebase_core_web_interop.dart';
import 'package:flutter/foundation.dart';
import 'package:http_parser/http_parser.dart';

import 'auth_interop.dart' as auth_interop;
Expand Down Expand Up @@ -392,9 +393,35 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
// ignore: close_sinks
StreamController<User?>? _changeController;

String get _authStateWindowsKey => 'flutterfire-${app.name}_authStateChanges';
String get _idTokenStateWindowsKey =>
'flutterfire-${app.name}_idTokenChanges';
// purely for debug mode and tracking listeners to clean up on "hot restart"
final Map<String, int> _authStateListeners = {};
String _authStateWindowsKey() {
if (kDebugMode) {
final key = 'flutterfire-${app.name}_authStateChanges';
if (_authStateListeners.containsKey(key)) {
_authStateListeners[key] = _authStateListeners[key]! + 1;
} else {
_authStateListeners[key] = 0;
}
return '$key-${_authStateListeners[key]}';
}
return 'no-op';
}

// purely for debug mode and tracking listeners to clean up on "hot restart"
final Map<String, int> _idTokenStateListeners = {};
String _idTokenStateWindowsKey() {
if (kDebugMode) {
final key = 'flutterfire-${app.name}_idTokenChanges';
if (_idTokenStateListeners.containsKey(key)) {
_idTokenStateListeners[key] = _idTokenStateListeners[key]! + 1;
} else {
_idTokenStateListeners[key] = 0;
}
return '$key-${_idTokenStateListeners[key]}';
}
return 'no-op';
}

/// Sends events when the users sign-in state changes.
///
Expand All @@ -403,7 +430,8 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
///
/// If the value is `null`, there is no signed-in user.
Stream<User?> get onAuthStateChanged {
unsubscribeWindowsListener(_authStateWindowsKey);
final authStateKey = _authStateWindowsKey();
unsubscribeWindowsListener(authStateKey);

if (_changeController == null) {
final nextWrapper = (auth_interop.UserJsImpl? user) {
Expand All @@ -417,14 +445,17 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
final unsubscribe =
jsObject.onAuthStateChanged(nextWrapper.toJS, errorWrapper.toJS);
_onAuthUnsubscribe = unsubscribe;
setWindowsListener(_authStateWindowsKey, unsubscribe);
setWindowsListener(
authStateKey,
unsubscribe,
);
}

void stopListen() {
_onAuthUnsubscribe!.callAsFunction();
_onAuthUnsubscribe = null;
_changeController = null;
removeWindowsListener(_authStateWindowsKey);
removeWindowsListener(authStateKey);
}

_changeController = StreamController<User?>.broadcast(
Expand All @@ -450,7 +481,8 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
///
/// If the value is `null`, there is no signed-in user.
Stream<User?> get onIdTokenChanged {
unsubscribeWindowsListener(_idTokenStateWindowsKey);
final idTokenKey = _idTokenStateWindowsKey();
unsubscribeWindowsListener(idTokenKey);
if (_idTokenChangedController == null) {
final nextWrapper = (auth_interop.UserJsImpl? user) {
_idTokenChangedController!.add(User.getInstance(user));
Expand All @@ -463,14 +495,17 @@ class Auth extends JsObjectWrapper<auth_interop.AuthJsImpl> {
final unsubscribe =
jsObject.onIdTokenChanged(nextWrapper.toJS, errorWrapper.toJS);
_onIdTokenChangedUnsubscribe = unsubscribe;
setWindowsListener(_idTokenStateWindowsKey, unsubscribe);
setWindowsListener(
idTokenKey,
unsubscribe,
);
}

void stopListen() {
_onIdTokenChangedUnsubscribe!.callAsFunction();
_onIdTokenChangedUnsubscribe = null;
_idTokenChangedController = null;
removeWindowsListener(_idTokenStateWindowsKey);
removeWindowsListener(idTokenKey);
}

_idTokenChangedController = StreamController<User?>.broadcast(
Expand Down
Loading