Describe the bug
After using package:google_sign_in (on web) to get a GoogleSignInAccount, then using that to get a GoogleSignInAuthentication, I cannot use that authentication as an argument for GoogleAuthProvider.credential. The error I get:
Error: Assertion failed: file:///C:/Users/levi/flutter/.pub-cache/hosted/pub.dartlang.org/firebase_auth_platform_interface-2.0.0/lib/src/providers/google_auth.dart:81:12
accessToken != null && idToken != null
"At least one of ID token and access token is required"
First off, notice how the error says at least one, while the assertion actually checks both. Second, I noticed removing this line allowed me to sign in. For context, the error here is that accessToken is null. I could post this issue for google_sign_in, but since I noticed this assertion doesn't seem to prevent me from actually logging in, I thought I'd post it here instead.
To Reproduce
Here's my code:
/// Signs in the user with Google as the provider.
static Future<void> signIn() async {
// THIS WORKS ON WEB, NOT MOBILE
//
// final UserCredential userCred = await auth.signInWithPopup(
// GoogleAuthProvider()
// );
// final OAuthCredential credential = userCred.credential as OAuthCredential;
// print("Successfully signed in");
// print(" idToken: ${credential.idToken}"); // not null
// print(" accessToken: ${credential.accessToken}"); // not null
// THIS WORKS ON MOBILE, NOT WEB
print("Signing in");
final GoogleSignInAccount googleAccount = await google.signIn();
print("Got Google account: $googleAccount");
if (googleAccount == null) {
return;
}
final GoogleSignInAuthentication googleAuth =
await googleAccount.authentication;
print("Got Google auth: $googleAuth");
print(" ID TOKEN: ${googleAuth.idToken}"); // not null
print(" Access Token: ${googleAuth.accessToken}"); // null
// THIS IS THE LINE THAT FAILS ON WEB
final GoogleAuthCredential credential = GoogleAuthProvider.credential (
accessToken: googleAuth.accessToken,
idToken: googleAuth.idToken
);
await auth.signInWithCredential(credential);
}
Expected behavior
Either accessToken should be non-null (as it is using signInWithPopup) or FIrebaseAuth should not check that since I am able to log in and download Firestore data just fine without it.
Additional context
Two things:
FirebaseAuth.signInWithPopup works just fine for web. I don't know why accessToken isn't null there, but the main difference is that using signInWithPopup, I don't need to call GoogleAuthProvider.credential, which has the assertion. Flutter doctor
Run flutter doctor and paste the output below:
[√] Flutter (Channel beta, 1.20.0, on Microsoft Windows [Version 10.0.18363.1016], locale en-US)
• Flutter version 1.20.0 at C:\Users\levi\flutter
• Framework revision 916c3ac648 (3 weeks ago), 2020-08-01 09:01:12 -0700
• Engine revision d6ee1499c2
• Dart version 2.9.0 (build 2.9.0-21.10.beta)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
• Android SDK at C:\Users\levi\AppData\Local\Android\sdk
• Platform android-29, build-tools 29.0.2
• Java binary at: C:\Program Files\Android\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
[√] Android Studio (version 3.5)
• Android Studio at C:\Program Files\Android
• Flutter plugin version 42.1.1
• Dart plugin version 191.8593
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
[√] Connected device (3 available)
• SM G950U1 (mobile) • 988861314d554d4f52 • android-arm64 • Android 9 (API 28)
• Web Server (web) • web-server • web-javascript • Flutter Tools
• Chrome (web) • chrome • web-javascript • Google Chrome 84.0.4147.125
• No issues found!
Also, here are my relevant dependencies:
firebase_auth: ^0.18.0
google_sign_in: ^4.5.1
Hey, please update to firebase_auth: ^0.18.0+1 - there was a bug with the assertion that was fixed.
Latest versions can be seen here: https://firebase.flutter.dev/docs/migration/#2-update-firebase-plugins
I tested and this works. Thanks for the quick fix!
Most helpful comment
Hey, please update to
firebase_auth: ^0.18.0+1- there was a bug with the assertion that was fixed.Latest versions can be seen here: https://firebase.flutter.dev/docs/migration/#2-update-firebase-plugins