Created
May 21, 2020 09:04
-
-
Save iampato/89c97d55c5cdb7f13d2a4d97d5514a67 to your computer and use it in GitHub Desktop.
Firebase Auth + Google sign in
This file contains hidden or 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 'dart:async'; | |
import 'package:firebase_auth/firebase_auth.dart'; | |
import 'package:google_sign_in/google_sign_in.dart'; | |
class UserRepository { | |
final FirebaseAuth _firebaseAuth; | |
final GoogleSignIn _googleSignIn; | |
UserRepository({FirebaseAuth firebaseAuth, GoogleSignIn googleSignin}) | |
: _firebaseAuth = firebaseAuth ?? FirebaseAuth.instance, | |
_googleSignIn = googleSignin ?? GoogleSignIn(); | |
Future<FirebaseUser> signInWithGoogle() async { | |
final GoogleSignInAccount googleUser = await _googleSignIn.signIn(); | |
final GoogleSignInAuthentication googleAuth = | |
await googleUser.authentication; | |
final AuthCredential credential = GoogleAuthProvider.getCredential( | |
accessToken: googleAuth.accessToken, | |
idToken: googleAuth.idToken, | |
); | |
await _firebaseAuth.signInWithCredential(credential); | |
print((await _firebaseAuth.currentUser()).providerData); | |
return _firebaseAuth.currentUser(); | |
} | |
Future<void> signOut() async { | |
return Future.wait([ | |
_firebaseAuth.signOut(), | |
_googleSignIn.signOut(), | |
]); | |
} | |
Future<bool> isSignedIn() async { | |
final currentUser = await _firebaseAuth.currentUser(); | |
return currentUser != null; | |
} | |
Future<FirebaseUser> getUser() async { | |
var user = await _firebaseAuth.currentUser(); | |
return user; | |
} | |
Future<String> getUserId() async { | |
return (await _firebaseAuth.currentUser()).uid; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment