Google Authentication
First we need to setup project in firebase console next Add below package name in pubspec.yaml google_sign_in: ^5.4.2 firebase_auth: ^3.11.2 firebase_core: ^1.24.0 firebase_messaging: ^13.1.0 package url: https://pub.dev/packages/google_sign_in
Code:
1.initialize firebase in main.dart file
await Firebase.initializeApp();
2.Login Screen
Loginfinal FirebaseAuth auth = FirebaseAuth.instance; final GoogleSignIn googleSignIn = GoogleSignIn(); For Login Add the below code: Future<void> signup(BuildContext context) async { final GoogleSignInAccount? googleSignInAccount = await googleSignIn.signIn(); if (googleSignInAccount != null) { final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication; final AuthCredential authCredential = GoogleAuthProvider.credential( idToken: googleSignInAuthentication.idToken, accessToken: googleSignInAuthentication.accessToken); // Getting users credential UserCredential result = await auth.signInWithCredential(authCredential); User? user = result.user; if(!mounted) return; Navigator.push(context, SlideRightRoute(page: LogoutScreen(user: user!))); } }
For signout:
await auth.signOut(); _googleSignIn.isSignedIn().then((s) async{ debugPrint("logout s: $s"); if(s){//true if user is logged in await _googleSignIn.signOut(); } });