Introduce the problem
I use an email to log in.
If I log out and try to log in to the same email using Google, the UID of the account changes. I can’t change back to the old UID. How can I provide multiple providers for an account?
What I tried
I asked this question to ChatGPT but it didn’t answer my question. I also googled this problem.
I have read this documentation and used its code, but it didn’t work. Not sure if I’m using its code correctly.
I’ve read this question but it didn’t help me.
A minimal, reproducible example
Future<void> signInWithEmailAndPassword(String email, String password) async {
try {
await firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
final credential = EmailAuthProvider.credential(email: email, password: password);
await FirebaseAuth.instance.currentUser?.linkWithCredential(credential);
} on FirebaseAuthException catch (e) {
throw FirebaseAuthException(code: e.code, message: e.message);
}
}
2
Answers
You should be able to achieve this via the Firebase Console. The Firebase Authentication Settings tab allows you to link accounts with the same email.
https://console.firebase.google.com/u/0/project/yourprojectname/authentication/settings
Account linking works by first having a currently signed in user, and then linking an additional provider to it. In steps:
currentUser
isn’t null.linkWithCredential
.