Authentication broke after updating FirebaseAuth
I had written a post on how to set up Authentication with Firebase in Flutter (see Authentication in Flutter). But Firebase auth has recently released a breaking change with v0.14. Here's how to fix it.
The change is that signInWithCredential
used to return a FirebaseUser
. Now it returns an AuthResult
(see documentation). Here's how to get the user. Old code:
final FirebaseUser user = await auth.signInWithCredential(credential);
New code:
final FirebaseUser user = (await auth.signInWithCredential(credential)).user;
Here's the complete snippet to handle Google Sign In. I pass in an instance of both FirebaseAuth and GoogleSignIn for easy mocking.
Future<AuthCredential> _signInWithGoogle(
FirebaseAuth auth, GoogleSignIn googleSignIn) async {
final GoogleSignInAccount googleUser = await googleSignIn.signIn();
final GoogleSignInAuthentication googleAuth = await googleUser.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken,
);
return credential;
}
Future<FirebaseUser> _handleSignIn(
FirebaseAuth auth, GoogleSignIn googleSignIn) async {
final credential = await _signInWithGoogle(auth, googleSignIn);
final FirebaseUser user = (await auth.signInWithCredential(credential)).user;
return user;
}