Skip to content

fix(ui_auth): automatically upgrade anonymous accounts #10071

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
Dec 8, 2022
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion packages/firebase_ui_auth/example/lib/main.dart
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion packages/firebase_ui_auth/lib/src/auth_flow.dart
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
}
}
}