I have a Flutter web based game wherein I am trying to convert anonymous user to google registered user in Firebase. I recently upgraded Flutter from v2.3 to v3.3.8 and corresponding Firebase packages too. Here are the upgraded versions of Firebase packages
firebase_core: ^2.1.1 firebase_auth: ^4.1.1 google_sign_in: ^5.4.1 firebase_database: ^10.0.4 firebase_analytics: ^10.0.4
By default when the user hits the web page he is signed in anonymously into Firebase. When the user clicks on the Sign in With Google button, I call the below function:
Future<bool> signInWithGoogle({required BuildContext context}) async {
UserCredential? gCredential;
if (kIsWeb) {
GoogleAuthProvider authProvider = GoogleAuthProvider();
try {
debugPrint('Inside sign in with Google');
gCredential = await anonUser?.linkWithPopup(authProvider);
debugPrint('After linkWithPopup');
guser = gCredential?.user;
FirebaseAuth.instance.setPersistence(Persistence
.LOCAL); // in order to maintain session when user closes browser and opens again
if (guser != null) {
email = guser?.email ?? '';
_token = await guser?.getIdToken();
gname = guser?.displayName ?? '';
guid = guser?.uid ?? '';
gimage = guser?.photoURL ?? '';
_userId = guid;
googleSignIn = true;
}
debugPrint('***Google user is $guser and name is $gname');
} on FirebaseAuthException catch (error) {
switch (error.code) {
case "provider-already-linked":
debugPrint("The provider has already been linked to the user.");
break;
case "invalid-credential":
debugPrint("The provider's credential is not valid.");
break;
case "credential-already-in-use":
{
debugPrint('In credential in use ${error.credential}');
if (error.credential != null) {
gCredential = await FirebaseAuth.instance
.signInWithCredential(error.credential!);
debugPrint('After sign in with credential');
guser = gCredential.user;
}
FirebaseAuth.instance.setPersistence(Persistence
.LOCAL); // in order to maintain session when user closes browser and opens again
if (guser != null) {
await guser?.reload(); // reloading to fix firebase bug
guser = FirebaseAuth.instance.currentUser;
email = guser?.email ?? '';
_token = await guser?.getIdToken();
gname = guser?.displayName ?? '';
guid = guser?.uid ?? '';
gimage = guser?.photoURL ?? '';
_userId = guid;
googleSignIn = true;
await readStats();
}
debugPrint(
"The account corresponding to the credential already exists, "
"or is already linked to a Firebase User $guser and ${anonUser?.uid}. ${error.email} ${error.credential} ${guser?.displayName}");
}
}
}
When I sign in, I am using an email that has already been used before so it is associated with some previous anonymous user in Firebase. Hence it goes into the credential-already-in-use exception. Here are the logs - you can see error.credential is coming null.
Inside sign in with Google
In credential in use null
The account corresponding to the credential already exists, or is already linked to a Firebase User null and N6ANc27jqGbFfp7zgPX9WmLktpp2. null null null
This same code was working perfectly fine before I upgraded. My earlier version of firebase packages was this:
firebase_core: 1.12.0 firebase_auth: ^3.3.6
With those packages, I was getting the error.credential and I could switch the user by signing in with the new credential.
This is the output of my flutter doctor -v:
[√] Flutter (Channel stable, 3.3.8, on Microsoft Windows [Version 10.0.19045.2251], locale en-IN)
• Flutter version 3.3.8 on channel stable at D:\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 52b3dc25f6 (5 days ago), 2022-11-09 12:09:26 +0800
• Engine revision 857bd6b74c
• Dart version 2.18.4
• DevTools version 2.15.0
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
• Android SDK at C:\Users\Radha\AppData\Local\Android\sdk
• Platform android-31, build-tools 30.0.3
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
• All Android licenses accepted.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Visual Studio - develop for Windows (Visual Studio Community 2022 17.0.6)
• Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
• Visual Studio Community 2022 version 17.0.32126.317
• Windows 10 SDK version 10.0.19041.0
[√] Android Studio (version 4.1)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] VS Code (version 1.73.1)
• VS Code at C:\Users\Radha\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.24.0
[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.2251]
• Chrome (web) • chrome • web-javascript • Google Chrome 107.0.5304.107
• Edge (web) • edge • web-javascript • Microsoft Edge 107.0.1418.42
[√] HTTP Host Availability
• All required HTTP hosts are available
• No issues found!
Appreciate if anyone can share pointers to help with this issue.