Skip to content

Commit

Permalink
fix(auth): deprecate FirebaseAuth.instanceFor's persistence param…
Browse files Browse the repository at this point in the history
…eter (#11259)
  • Loading branch information
exaby73 committed Sep 18, 2023
1 parent 251d15e commit a1966e8
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 57 deletions.
22 changes: 10 additions & 12 deletions packages/firebase_auth/firebase_auth/lib/src/firebase_auth.dart
Expand Up @@ -15,10 +15,6 @@ class FirebaseAuth extends FirebasePluginPlatform {
// instance with the default app before a user specifies an app.
FirebaseAuthPlatform? _delegatePackingProperty;

// To set "persistence" on web, it is now required on the v9.0.0 or above Firebase JS SDK to pass the value on calling `initializeAuth()`.
/// https://firebase.google.com/docs/reference/js/auth.md#initializeauth
Persistence? _persistence;

/// Returns the underlying delegate implementation.
///
/// If called and no [_delegatePackingProperty] exists, it will first be
Expand All @@ -27,17 +23,15 @@ class FirebaseAuth extends FirebasePluginPlatform {
_delegatePackingProperty ??= FirebaseAuthPlatform.instanceFor(
app: app,
pluginConstants: pluginConstants,
persistence: _persistence,
);
return _delegatePackingProperty!;
}

/// The [FirebaseApp] for this current Auth instance.
FirebaseApp app;

FirebaseAuth._({required this.app, Persistence? persistence})
: _persistence = persistence,
super(app.name, 'plugins.flutter.io/firebase_auth');
FirebaseAuth._({required this.app})
: super(app.name, 'plugins.flutter.io/firebase_auth');

/// Returns an instance using the default [FirebaseApp].
static FirebaseAuth get instance {
Expand All @@ -47,11 +41,15 @@ class FirebaseAuth extends FirebasePluginPlatform {
}

/// Returns an instance using a specified [FirebaseApp].
/// Note that persistence can only be used on Web and is not supported on other platforms.
factory FirebaseAuth.instanceFor(
{required FirebaseApp app, Persistence? persistence}) {
factory FirebaseAuth.instanceFor({
required FirebaseApp app,
@Deprecated(
'Will be removed in future release. Use setPersistence() instead.',
)
Persistence? persistence,
}) {
return _firebaseAuthInstances.putIfAbsent(app.name, () {
return FirebaseAuth._(app: app, persistence: persistence);
return FirebaseAuth._(app: app);
});
}

Expand Down
Expand Up @@ -189,8 +189,7 @@ class MethodChannelFirebaseAuth extends FirebaseAuthPlatform {
///
/// Instances are cached and reused for incoming event handlers.
@override
FirebaseAuthPlatform delegateFor(
{required FirebaseApp app, Persistence? persistence}) {
FirebaseAuthPlatform delegateFor({required FirebaseApp app}) {
return methodChannelFirebaseAuthInstances.putIfAbsent(app.name, () {
return MethodChannelFirebaseAuth(app: app);
});
Expand Down
Expand Up @@ -51,17 +51,14 @@ abstract class FirebaseAuthPlatform extends PlatformInterface {
factory FirebaseAuthPlatform.instanceFor({
required FirebaseApp app,
required Map<dynamic, dynamic> pluginConstants,
Persistence? persistence,
}) {
var currentUser = pluginConstants['APP_CURRENT_USER'];

if (currentUser != null) {
currentUser as List<Object?>;
currentUser = PigeonUserDetails.decode(currentUser);
}
return FirebaseAuthPlatform.instance
.delegateFor(app: app, persistence: persistence)
.setInitialValues(
return FirebaseAuthPlatform.instance.delegateFor(app: app).setInitialValues(
languageCode: pluginConstants['APP_LANGUAGE_CODE'],
currentUser: currentUser,
);
Expand Down Expand Up @@ -89,8 +86,7 @@ abstract class FirebaseAuthPlatform extends PlatformInterface {
///
/// Setting a [persistence] type is only available on web based platforms.
@protected
FirebaseAuthPlatform delegateFor(
{required FirebaseApp app, Persistence? persistence}) {
FirebaseAuthPlatform delegateFor({required FirebaseApp app}) {
throw UnimplementedError('delegateFor() is not implemented');
}

Expand Down
Expand Up @@ -34,14 +34,8 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {

Completer<void> _initialized = Completer();

// To set "persistence" on web, it is now required on the v9.0.0 or above Firebase JS SDK to pass the value on calling `initializeAuth()`.
// https://firebase.google.com/docs/reference/js/auth.md#initializeauth
Persistence? _persistence;

/// The entry point for the [FirebaseAuthWeb] class.
FirebaseAuthWeb({required FirebaseApp app, Persistence? persistence})
: super(appInstance: app) {
_persistence = persistence;
FirebaseAuthWeb({required FirebaseApp app}) : super(appInstance: app) {
// Create a app instance broadcast stream for both delegate listener events
_userChangesListeners[app.name] =
StreamController<UserPlatform?>.broadcast();
Expand Down Expand Up @@ -148,16 +142,14 @@ class FirebaseAuthWeb extends FirebaseAuthPlatform {
auth_interop.Auth? _webAuth;

auth_interop.Auth get delegate {
_webAuth ??= auth_interop.getAuthInstance(core_interop.app(app.name),
persistence: _persistence);
_webAuth ??= auth_interop.getAuthInstance(core_interop.app(app.name));

return _webAuth!;
}

@override
FirebaseAuthPlatform delegateFor(
{required FirebaseApp app, Persistence? persistence}) {
return FirebaseAuthWeb(app: app, persistence: persistence);
FirebaseAuthPlatform delegateFor({required FirebaseApp app}) {
return FirebaseAuthWeb(app: app);
}

@override
Expand Down
Expand Up @@ -20,31 +20,7 @@ import 'utils/utils.dart';
export 'auth_interop.dart';

/// Given an AppJSImp, return the Auth instance.
Auth getAuthInstance(App app, {Persistence? persistence}) {
if (persistence != null) {
auth_interop.Persistence setPersistence;
switch (persistence) {
case Persistence.LOCAL:
setPersistence = auth_interop.browserLocalPersistence;
break;
case Persistence.INDEXED_DB:
setPersistence = auth_interop.indexedDBLocalPersistence;
break;
case Persistence.SESSION:
setPersistence = auth_interop.browserSessionPersistence;
break;
case Persistence.NONE:
setPersistence = auth_interop.inMemoryPersistence;
break;
}
return Auth.getInstance(auth_interop.initializeAuth(
app.jsObject,
jsify({
'errorMap': auth_interop.debugErrorMap,
'persistence': setPersistence,
'popupRedirectResolver': auth_interop.browserPopupRedirectResolver
})));
}
Auth getAuthInstance(App app) {
return Auth.getInstance(auth_interop.initializeAuth(
app.jsObject,
jsify({
Expand Down

0 comments on commit a1966e8

Please sign in to comment.