Created
April 23, 2020 12:38
-
-
Save rawnly/431fcee67bdf23a6ffbcd0a0ee13335e to your computer and use it in GitHub Desktop.
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:firebase_auth/firebase_auth.dart'; | |
import 'package:flutter/services.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
class SignInUtility { | |
static final FirebaseAuth _auth = FirebaseAuth.instance; | |
static final GoogleSignIn googleSignIn = GoogleSignIn(); | |
static Future<FirebaseUser> signInWithGoogle() async { | |
final GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn(); | |
if (googleSignInAccount == null) { | |
// Sign in flow canceled. | |
return null; | |
} | |
final GoogleSignInAuthentication googleSignInAuthentication = await googleSignInAccount.authentication; | |
final AuthCredential credential = GoogleAuthProvider.getCredential( | |
accessToken: googleSignInAuthentication.accessToken, | |
idToken: googleSignInAuthentication.idToken, | |
); | |
final AuthResult authResult = await _auth.signInWithCredential(credential); | |
final FirebaseUser user = authResult.user; | |
assert(!user.isAnonymous); | |
assert(await user.getIdToken() != null); | |
final FirebaseUser currentUser = await _auth.currentUser(); | |
assert(user.uid == currentUser.uid); | |
print("Sign in completed. Welcome ${user.displayName}"); | |
return user; | |
} | |
static signOutGoogle() async { | |
await googleSignIn.signOut(); | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment