Flutter / Firebase google sign in skips normal flow

Viewed 135

I'm having some problems with google sign in. It works - it signs me in but the screen just kind of flashes and the sign in flow where you'd choose your account never appears, then it just signs me in with my main google account. Not sure what I'm doing wrong.

Its not normal behavior. I think normally a little dialog pops up and you get the chance to choose what google account you want to sign in with. This is the part that is not happening. I even tried on my phone, with a never installed before apk and never saw that dialog but it signed me with the default account.

Any help greatly appreciated:

general_providers.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:riverpod/riverpod.dart';

final firebaseAuthProvider =
    Provider<FirebaseAuth>((ref) => FirebaseAuth.instance);

final authStateChangesProvider = StreamProvider<User?>(
    (ref) => ref.watch(firebaseAuthProvider).authStateChanges());

auth_widget.dart. -> this one is meant to just display the home page or login page depending on auth state:

class AuthWidget extends ConsumerWidget {
  const AuthWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final authStateChanges = ref.watch(authStateChangesProvider);
    return authStateChanges.when(
      data: (user) => user != null ? const HomePage() : LoginPage(),
      loading: () => const Center(child: CircularProgressIndicator()),
      error: (error, __) => Scaffold(
        body: Center(
          child: Text(error.toString()),
        ),
      ),
    );
  }
}

firebase_auth_service.dart

final authServiceProvider =
    Provider<FirebaseAuthService>((ref) => FirebaseAuthService(ref.read));

class FirebaseAuthService {
  FirebaseAuthService(this._read);

  final Reader _read;

//TODO add in try / catch with firebase exeptions

  Future<void> signInAnonymously() async {
    await _read(firebaseAuthProvider).signInAnonymously();
  }

  User? getCurrentUser() => _read(firebaseAuthProvider).currentUser;

  Future<void> signOut() async {
    await _read(firebaseAuthProvider).signOut();
  }

  Future<UserCredential> signInWithGoogle() async {
    try {
      // await _googleSignIn.signIn();
      final GoogleSignInAccount? googleUser = await GoogleSignIn().signIn();
      // Obtain the auth details from the request
      final GoogleSignInAuthentication? googleAuth =
          await googleUser?.authentication;
      // Create a new credential
      final credential = GoogleAuthProvider.credential(
        accessToken: googleAuth?.accessToken,
        idToken: googleAuth?.idToken,
      );

      return await _read(firebaseAuthProvider).signInWithCredential(credential);
    } catch (error) {
      throw FirebaseAuthException(code: error.toString());
    }
  }
}

The signInWithGoogle method above is pretty much copy paste from the docs except that I link my firebaseAuthProvider in the final line to get that top level auth instance. When I run this method from the signin page it does sign me in but just skips the normal flow. Any ideas?

1 Answers

I think this is the expected behavior. You would need to log out before you get to select account again.

Related