Skip to content

Commit b18a346

Browse files
committed
fix(ui_auth): automatically upgrade anonymous accounts
1 parent a0910a1 commit b18a346

File tree

9 files changed

+47
-29
lines changed

9 files changed

+47
-29
lines changed

packages/firebase_ui_auth/example/lib/main.dart

+12-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Future<void> main() async {
3333
WidgetsFlutterBinding.ensureInitialized();
3434
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
3535

36+
if (FirebaseAuth.instance.currentUser == null) {
37+
await FirebaseAuth.instance.signInAnonymously();
38+
}
39+
3640
FirebaseUIAuth.configureProviders([
3741
EmailAuthProvider(),
3842
emailLinkProviderConfig,
@@ -66,7 +70,7 @@ class FirebaseAuthUIExample extends StatelessWidget {
6670
String get initialRoute {
6771
final auth = FirebaseAuth.instance;
6872

69-
if (auth.currentUser == null) {
73+
if (auth.currentUser == null || auth.currentUser!.isAnonymous) {
7074
return '/';
7175
}
7276

@@ -139,6 +143,13 @@ class FirebaseAuthUIExample extends StatelessWidget {
139143
Navigator.pushReplacementNamed(context, '/profile');
140144
}
141145
}),
146+
AuthStateChangeAction<CredentialLinked>((context, state) {
147+
if (!state.user.emailVerified) {
148+
Navigator.pushNamed(context, '/verify-email');
149+
} else {
150+
Navigator.pushReplacementNamed(context, '/profile');
151+
}
152+
}),
142153
mfaAction,
143154
EmailLinkSignInAction((context) {
144155
Navigator.pushReplacementNamed(context, '/email-link-sign-in');

packages/firebase_ui_auth/example/linux/flutter/generated_plugin_registrant.cc

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
2-
// for details. All rights reserved. Use of this source code is governed by a
3-
// BSD-style license that can be found in the LICENSE file.
4-
51
//
62
// Generated file. Do not edit.
73
//

packages/firebase_ui_auth/example/linux/flutter/generated_plugin_registrant.h

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
* Copyright 2022, the Chromium project authors. Please see the AUTHORS file
3-
* for details. All rights reserved. Use of this source code is governed by a
4-
* BSD-style license that can be found in the LICENSE file.
5-
*/
6-
71
//
82
// Generated file. Do not edit.
93
//

packages/firebase_ui_auth/example/windows/flutter/generated_plugin_registrant.cc

-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
2-
// for details. All rights reserved. Use of this source code is governed by a
3-
// BSD-style license that can be found in the LICENSE file.
4-
51
//
62
// Generated file. Do not edit.
73
//

packages/firebase_ui_auth/example/windows/flutter/generated_plugin_registrant.h

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
* Copyright 2022, the Chromium project authors. Please see the AUTHORS file
3-
* for details. All rights reserved. Use of this source code is governed by a
4-
* BSD-style license that can be found in the LICENSE file.
5-
*/
6-
71
//
82
// Generated file. Do not edit.
93
//

packages/firebase_ui_auth/lib/src/auth_flow.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class AuthFlow<T extends AuthProvider> extends ValueNotifier<AuthState>
126126

127127
@override
128128
void onCredentialLinked(AuthCredential credential) {
129-
value = CredentialLinked(credential);
129+
value = CredentialLinked(credential, auth.currentUser!);
130130
}
131131

132132
@override

packages/firebase_ui_auth/lib/src/auth_state.dart

+4-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,11 @@ class CredentialLinked extends AuthState {
107107
/// A credential that was linked with the currently signed in user account.
108108
final AuthCredential credential;
109109

110+
/// An instance of the [User] the credential was associated with.
111+
final User user;
112+
110113
/// {@macro ui.auth.auth_state.credential_linked}
111-
CredentialLinked(this.credential);
114+
CredentialLinked(this.credential, this.user);
112115
}
113116

114117
/// {@template ui.auth.auth_state.auth_failed}

packages/firebase_ui_auth/lib/src/providers/auth_provider.dart

+12-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,12 @@ abstract class AuthProvider<T extends AuthListener, K extends AuthCredential> {
109109
/// {@macro ui.auth.auth_provider}
110110
AuthProvider();
111111

112+
/// Indicates whether the user should be upgraded and new credential should be
113+
/// linked.
114+
bool get shouldUpgradeAnonymous => auth.currentUser?.isAnonymous ?? false;
115+
112116
/// Signs the user in with the provided [AuthCredential].
113-
void signInWithCredential(AuthCredential credential) {
117+
void signInWithCredential(K credential) {
114118
authListener.onBeforeSignIn();
115119
auth
116120
.signInWithCredential(credential)
@@ -120,8 +124,9 @@ abstract class AuthProvider<T extends AuthListener, K extends AuthCredential> {
120124

121125
/// Links a provided [AuthCredential] with the currently signed in user
122126
/// account.
123-
void linkWithCredential(AuthCredential credential) {
127+
void linkWithCredential(K credential) {
124128
authListener.onCredentialReceived(credential);
129+
125130
try {
126131
final user = auth.currentUser!;
127132
user
@@ -175,6 +180,11 @@ abstract class AuthProvider<T extends AuthListener, K extends AuthCredential> {
175180
// Only email provider has a different action for sign in and sign up
176181
// and implements it's own sign up logic.
177182
case AuthAction.signUp:
183+
if (shouldUpgradeAnonymous) {
184+
linkWithCredential(credential);
185+
break;
186+
}
187+
178188
signInWithCredential(credential);
179189
break;
180190
case AuthAction.none:

packages/firebase_ui_auth/lib/src/providers/email_auth_provider.dart

+18-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class EmailAuthProvider
2626
/// Tries to create a new user account with the given [EmailAuthCredential].
2727
void signUpWithCredential(fba.EmailAuthCredential credential) {
2828
authListener.onBeforeSignIn();
29+
2930
auth
3031
.createUserWithEmailAndPassword(
3132
email: credential.email,
@@ -55,10 +56,23 @@ class EmailAuthProvider
5556
fba.EmailAuthCredential credential,
5657
AuthAction action,
5758
) {
58-
if (action == AuthAction.signUp) {
59-
signUpWithCredential(credential);
60-
} else {
61-
super.onCredentialReceived(credential, action);
59+
switch (action) {
60+
case AuthAction.signIn:
61+
signInWithCredential(credential);
62+
break;
63+
case AuthAction.signUp:
64+
if (shouldUpgradeAnonymous) {
65+
return linkWithCredential(credential);
66+
}
67+
68+
signUpWithCredential(credential);
69+
break;
70+
case AuthAction.link:
71+
linkWithCredential(credential);
72+
break;
73+
case AuthAction.none:
74+
super.onCredentialReceived(credential, action);
75+
break;
6276
}
6377
}
6478
}

0 commit comments

Comments
 (0)