Skip to content

Commit

Permalink
fix(ui_auth): automatically upgrade anonymous accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnitsky committed Dec 7, 2022
1 parent a0910a1 commit c9393b6
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 29 deletions.
13 changes: 12 additions & 1 deletion packages/firebase_ui_auth/example/lib/main.dart
Expand Up @@ -33,6 +33,10 @@ Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

if (FirebaseAuth.instance.currentUser == null) {
await FirebaseAuth.instance.signInAnonymously();
}

FirebaseUIAuth.configureProviders([
EmailAuthProvider(),
emailLinkProviderConfig,
Expand Down Expand Up @@ -66,7 +70,7 @@ class FirebaseAuthUIExample extends StatelessWidget {
String get initialRoute {
final auth = FirebaseAuth.instance;

if (auth.currentUser == null) {
if (auth.currentUser == null || auth.currentUser!.isAnonymous) {
return '/';
}

Expand Down Expand Up @@ -139,6 +143,13 @@ class FirebaseAuthUIExample extends StatelessWidget {
Navigator.pushReplacementNamed(context, '/profile');
}
}),
AuthStateChangeAction<CredentialLinked>((context, state) {
if (!state.user.emailVerified) {
Navigator.pushNamed(context, '/verify-email');
} else {
Navigator.pushReplacementNamed(context, '/profile');
}
}),
mfaAction,
EmailLinkSignInAction((context) {
Navigator.pushReplacementNamed(context, '/email-link-sign-in');
Expand Down
@@ -1,7 +1,3 @@
// Copyright 2022, the Chromium project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

//
// Generated file. Do not edit.
//
Expand Down
@@ -1,9 +1,3 @@
/*
* Copyright 2022, the Chromium project authors. Please see the AUTHORS file
* for details. All rights reserved. Use of this source code is governed by a
* BSD-style license that can be found in the LICENSE file.
*/

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

//
// Generated file. Do not edit.
//
Expand Down
@@ -1,9 +1,3 @@
/*
* Copyright 2022, the Chromium project authors. Please see the AUTHORS file
* for details. All rights reserved. Use of this source code is governed by a
* BSD-style license that can be found in the LICENSE file.
*/

//
// Generated file. Do not edit.
//
Expand Down
2 changes: 1 addition & 1 deletion packages/firebase_ui_auth/lib/src/auth_flow.dart
Expand Up @@ -126,7 +126,7 @@ class AuthFlow<T extends AuthProvider> extends ValueNotifier<AuthState>

@override
void onCredentialLinked(AuthCredential credential) {
value = CredentialLinked(credential);
value = CredentialLinked(credential, auth.currentUser!);
}

@override
Expand Down
5 changes: 4 additions & 1 deletion packages/firebase_ui_auth/lib/src/auth_state.dart
Expand Up @@ -107,8 +107,11 @@ class CredentialLinked extends AuthState {
/// A credential that was linked with the currently signed in user account.
final AuthCredential credential;

/// An instance of the [User] the credential was associated with.
final User user;

/// {@macro ui.auth.auth_state.credential_linked}
CredentialLinked(this.credential);
CredentialLinked(this.credential, this.user);
}

/// {@template ui.auth.auth_state.auth_failed}
Expand Down
14 changes: 12 additions & 2 deletions packages/firebase_ui_auth/lib/src/providers/auth_provider.dart
Expand Up @@ -109,8 +109,12 @@ abstract class AuthProvider<T extends AuthListener, K extends AuthCredential> {
/// {@macro ui.auth.auth_provider}
AuthProvider();

/// Indicates whether the user should be upgraded and new credential should be
/// linked.
bool get shouldUpgradeAnonymous => auth.currentUser?.isAnonymous ?? false;

/// Signs the user in with the provided [AuthCredential].
void signInWithCredential(AuthCredential credential) {
void signInWithCredential(K credential) {
authListener.onBeforeSignIn();
auth
.signInWithCredential(credential)
Expand All @@ -120,8 +124,9 @@ abstract class AuthProvider<T extends AuthListener, K extends AuthCredential> {

/// Links a provided [AuthCredential] with the currently signed in user
/// account.
void linkWithCredential(AuthCredential credential) {
void linkWithCredential(K credential) {
authListener.onCredentialReceived(credential);

try {
final user = auth.currentUser!;
user
Expand Down Expand Up @@ -175,6 +180,11 @@ abstract class AuthProvider<T extends AuthListener, K extends AuthCredential> {
// Only email provider has a different action for sign in and sign up
// and implements it's own sign up logic.
case AuthAction.signUp:
if (shouldUpgradeAnonymous) {
linkWithCredential(credential);
break;
}

signInWithCredential(credential);
break;
case AuthAction.none:
Expand Down
Expand Up @@ -55,10 +55,23 @@ class EmailAuthProvider
fba.EmailAuthCredential credential,
AuthAction action,
) {
if (action == AuthAction.signUp) {
signUpWithCredential(credential);
} else {
super.onCredentialReceived(credential, action);
switch (action) {
case AuthAction.signIn:
signInWithCredential(credential);
break;
case AuthAction.signUp:
if (shouldUpgradeAnonymous) {
return linkWithCredential(credential);
}

signUpWithCredential(credential);
break;
case AuthAction.link:
linkWithCredential(credential);
break;
case AuthAction.none:
super.onCredentialReceived(credential, action);
break;
}
}
}

0 comments on commit c9393b6

Please sign in to comment.