firebase / FirebaseUI-Flutter

Apache License 2.0
107 stars 100 forks source link

[firebase_ui_auth] Emit `SignedIn` event after `UserCreated` #382

Open Rexios80 opened 2 months ago

Rexios80 commented 2 months ago

Is there an existing issue for this?

What plugin is this bug for?

Firebase UI Auth

What platform(s) does this bug affect?

No response

List of dependencies used.

Not relevant

Steps to reproduce

Expected Behavior

SignedIn event is sent after UserCreated event

Actual Behavior

SignedIn event is not sent after UserCreated event

Additional Information

No response

Rexios80 commented 2 months ago

Not exactly sure if we want this behavior or not, but if this is intended it needs better documentation

russellwheatley commented 1 month ago

@Rexios80 - Please provide a code sample for me to drop in to example app so I know specifically what you're referring to. Thanks.

Rexios80 commented 1 month ago

@russellwheatley The example code in the readme has this issue: https://pub.dev/packages/firebase_ui_auth

Using that code, the app does not go to the signed in page after registering a new user because there is no handler for the UserCreated event

jsenitza commented 1 month ago

I'm running into this as well - my newState goes from SigningIn to UserCreated and then never throws the SignedIn status. I'm assuming I'm safe to stop at UserCreated but this tripped me up as well as the documentation that @Rexios80 posted is what I was following, waiting for the status to hit SignedIn.

AuthStateListener<OAuthController>(
                child: OAuthProviderButton(
                    provider: GoogleProvider(
                        clientId:
                            'OMITTED.apps.googleusercontent.com')),
                listener: (oldState, newState, ctrl) {
                  dPrint('Old state: $oldState, New state: $newState');
                  if (newState is SigningIn) {
                    dPrint('Signing in...');
                  } else if (newState is UserCreated) {
                    dPrint('User created...');
                  } else if (newState is SignedIn) {
                    dPrint('Signed in with Google');
                    if (context.mounted) {
                      dPrint('Navigating to finish profile page');
                      Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                          builder: (context) => const OauthFinishProfilePage(),
                        ),
                      );
                    }
                  } else if (newState is AuthFailed) {
                    dPrint('Authentication failed: ${newState.exception}');
                  }
                  return null;
                },
              ),

Here's what this outputs when I login using Google:

flutter: Old state: Instance of 'Uninitialized', New state: Instance of 'SigningIn'
flutter: Signing in...
flutter: Old state: Instance of 'SigningIn', New state: Instance of 'UserCreated'
flutter: User created...
russellwheatley commented 3 days ago

@Rexios80 - this is intentional behaviour which you can find here: https://github.com/firebase/FirebaseUI-Flutter/blob/auth-353/packages/firebase_ui_auth/lib/src/auth_flow.dart#L149-L156

When user is signed-in, it is one or the other value depending on whether the user is new or not.

You're correct though, perhaps it could be better documented though. We happily accept PRs if you want to update the documentation:

https://github.com/firebase/FirebaseUI-Flutter/blob/main/docs/firebase-ui-auth/providers/email.md?plain=1#L103-L105 https://github.com/firebase/FirebaseUI-Flutter/blob/main/docs/firebase-ui-auth/providers/email-link.md?plain=1#L116 https://github.com/firebase/FirebaseUI-Flutter/blob/main/docs/firebase-ui-auth/providers/oauth.md?plain=1#L224 https://github.com/firebase/FirebaseUI-Flutter/blob/main/docs/firebase-ui-auth/providers/phone.md?plain=1#L130