Skip to content

Instantly share code, notes, and snippets.

@rawnly
Created April 23, 2020 12:38
Show Gist options
  • Save rawnly/431fcee67bdf23a6ffbcd0a0ee13335e to your computer and use it in GitHub Desktop.
Save rawnly/431fcee67bdf23a6ffbcd0a0ee13335e to your computer and use it in GitHub Desktop.
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