You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, I had a specific case in our application that I was struggling with. There are many StackOverflow questions regarding this so I thought I'd post my approach and proposal here.
The problem:
My mobile app creates an anonymous user on the first launch. Later when the user wants to create an account, their oauth credentials are linked with the anonymous user. No problem here.
But the problem starts when they sign out or reinstall the application.
In such case, a new anonymous user is created. Then when the user wants to log in with his oauth again, there is an error message "This provider is associated with another account".
This is because the library is trying to link the new anonymous account with credentials that are already linked with the first anonymous.
My approach:
I think that when user tries to sign in with an already linked OAuth, then they just want to sign back into their past account, instead of linking to a new fresh installation of the app. However, I haven't found an explicit configuration for such behavior in the library.
After some digging I found a way to override the default behavior per AuthProvider:
classGoogleProviderFixextendsGoogleProvider {
GoogleProviderFix({requiredsuper.clientId});
@overridevoidlinkWithCredential(fba.OAuthCredential credential) async {
authListener.onCredentialReceived(credential);
try {
final user = auth.currentUser!;
try {
await user.linkWithCredential(credential);
authListener.onCredentialLinked(credential);
} on fba.FirebaseAuthExceptioncatch (err) {
// The original code simply throws when it runs into this particular error.// But we'd rather try to sign in to that already existing account.if (err.code =='credential-already-in-use') {
signInWithCredential(credential);
} else {
rethrow;
}
}
} catch (err) {
authListener.onError(err);
}
}
}
That did the trick for me. Although it's kinda dumb that this will always lead to failure first for everybody that signs in to an existing account. Would be nice to have this approached from the backend side.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello, I had a specific case in our application that I was struggling with. There are many StackOverflow questions regarding this so I thought I'd post my approach and proposal here.
The problem:
My mobile app creates an anonymous user on the first launch. Later when the user wants to create an account, their oauth credentials are linked with the anonymous user. No problem here.
But the problem starts when they sign out or reinstall the application.
In such case, a new anonymous user is created. Then when the user wants to log in with his oauth again, there is an error message "This provider is associated with another account".
This is because the library is trying to link the new anonymous account with credentials that are already linked with the first anonymous.
My approach:
I think that when user tries to sign in with an already linked OAuth, then they just want to sign back into their past account, instead of linking to a new fresh installation of the app. However, I haven't found an explicit configuration for such behavior in the library.
After some digging I found a way to override the default behavior per AuthProvider:
That did the trick for me. Although it's kinda dumb that this will always lead to failure first for everybody that signs in to an existing account. Would be nice to have this approached from the backend side.
Beta Was this translation helpful? Give feedback.
All reactions