Created
April 23, 2020 03:19
-
-
Save Ahmadre/e86c7686bb3ef7d0795642a0c585cb0a to your computer and use it in GitHub Desktop.
Google Sign In Flutter Web
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:google_sign_in/google_sign_in.dart'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
final GoogleSignIn _googleAuth = GoogleSignIn(); | |
final FirebaseAuth _auth = FirebaseAuth.instance; | |
Future<dynamic> _signInWithGoogle({BuildContext ctx}) async { | |
final GoogleSignInAccount googleUser = await _googleAuth.signIn(); | |
if (googleUser == null) { | |
Scaffold.of(ctx).showSnackBar(SnackBar( | |
action: SnackBarAction( | |
label: 'Close', | |
onPressed: () => Scaffold.of(ctx).hideCurrentSnackBar(), | |
), | |
content: AutoSizeText('Canceled by user', | |
maxLines: 1, style: TextStyle(fontSize: 14)))); | |
return; | |
} | |
final GoogleSignInAuthentication googleAuth = | |
await googleUser.authentication; | |
final AuthCredential credential = GoogleAuthProvider.getCredential( | |
accessToken: googleAuth.accessToken, | |
idToken: googleAuth.idToken, | |
); | |
final FirebaseUser user = | |
(await _auth.signInWithCredential(credential).catchError((e) { | |
debugPrint(e); | |
})) | |
.user; | |
return user; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment