Skip to content

Instantly share code, notes, and snippets.

@Shafran123
Created June 4, 2019 06:09
Show Gist options
  • Save Shafran123/fba67cdd44df331b283a3cead80302ef to your computer and use it in GitHub Desktop.
Save Shafran123/fba67cdd44df331b283a3cead80302ef to your computer and use it in GitHub Desktop.
Example code of how to sign in with google.
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:firebase_auth/firebase_auth.dart';
void _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,
);
final FirebaseUser user = await _auth.signInWithCredential(credential);
assert(user.email != null);
assert(user.displayName != null);
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
final FirebaseUser currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
setState(() {
if (user != null) {
_success = true;
_userID = user.uid;
_email = user.email;
_name = user.displayName;
_ppic = user.photoUrl;
return _userID;
} else {
_success = false;
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment