Kevnlan
Kevnlan

Reputation: 567

Flutter - Firebase Google authentication

I am trying to enable Google authentication using Firebase, I have added the required plugins as indicated and modified gradle files as needed but I cannot seem to login to the app successfully.

This is what I get printed to the command line:

W/System  (27940): Ignoring header X-Firebase-Locale because its value was null.
I/System.out(27940): [okhttp]:check permission begin!
I/System.out(27940): [okhttp]:not MMS!
I/System.out(27940): [okhttp]:not Email!
I/System.out(27940): [OkHttp] sendRequest<<
W/System  (27940): Ignoring header X-Firebase-Locale because its value was null.
I/System.out(27940): [okhttp]:check permission begin!
I/System.out(27940): [okhttp]:not MMS!
I/System.out(27940): [okhttp]:not Email!
I/System.out(27940): [OkHttp] sendRequest<<
D/FirebaseAuth(27940): Notifying id token listeners about user ( JWKcjMq91nOvkHNdIuim12ahbm53 ).

Here is the code for authenticating a user:

class GoogleSignInProvider extends ChangeNotifier {
  final googleSignIn = GoogleSignIn();
  GoogleSignInAccount? _user;
  GoogleSignInAccount get user => _user!;

  Future googleLogin() async {
    final googleUser = await googleSignIn.signIn();

    if (googleUser == null) return;
    _user = googleUser;
    final googleAuth = await googleUser.authentication;
    final credential = GoogleAuthProvider.credential(
      accessToken: googleAuth.accessToken,
      idToken: googleAuth.idToken,
    );

    await FirebaseAuth.instance.signInWithCredential(credential);
    notifyListeners();
  }
}

Here is the code for listening if a user has been authenticated:

      body: StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return const CircularProgressIndicator();
          } else if (snapshot.hasError) {
            return const Center(
              child: Text('An error has occured'),
            );
          } else if (snapshot.hasData) {
            return DisplayPage();
          } else {
            return DisplayPage();
          }
        },
      ),

And this is my main class code:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => GoogleSignInProvider(),
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: LoginPage(),
      ),
    );
  }
}

Upvotes: 0

Views: 227

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12353

You are being signed in and the auth process is working, look at your debug console output, last line: Notifying id token listeners about user ( JWKcjMq91nOvkHNdIuim12ahbm53 )

This means that you are signed in.

Upvotes: 1

Related Questions