Skip to content

Commit 71bfedb

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

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
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/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

+17-4
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,23 @@ class EmailAuthProvider
5555
fba.EmailAuthCredential credential,
5656
AuthAction action,
5757
) {
58-
if (action == AuthAction.signUp) {
59-
signUpWithCredential(credential);
60-
} else {
61-
super.onCredentialReceived(credential, action);
58+
switch (action) {
59+
case AuthAction.signIn:
60+
signInWithCredential(credential);
61+
break;
62+
case AuthAction.signUp:
63+
if (shouldUpgradeAnonymous) {
64+
return linkWithCredential(credential);
65+
}
66+
67+
signUpWithCredential(credential);
68+
break;
69+
case AuthAction.link:
70+
linkWithCredential(credential);
71+
break;
72+
case AuthAction.none:
73+
super.onCredentialReceived(credential, action);
74+
break;
6275
}
6376
}
6477
}

0 commit comments

Comments
 (0)