When working with Firebase, I am faced with the challenge of checking if a Facebook user exists without automatically creating a new user account. The situation arises when an anonymous user attempts to log in using their Facebook credentials, and I want this login attempt to fail if the Facebook account is not already linked to an existing user in my system.
My expectation was that using Auth.signInAndRetrieveDataWithCredential would result in an "auth/user-not-found" error if no matching user was found. However, instead of returning the expected error, a new user account is created. Is this behavior a bug or is it intentional?
let credential = firebase.auth.FacebookAuthProvider.credential(
event.authResponse.accessToken)
firebase.auth().signInAndRetrieveDataWithCredential(credential).then( (userCredential) => {
let user = userCredential.user
app.debug("DEBUG: Existing user signed in:"+user.uid)
this.loginSuccess(user)
}).catch( (err) => {
app.error("ERROR re-signing in:"+err.code)
$("#login_status_msg").text(err)
})
On the other hand, using User.reauthenticateAndRetrieveDataWithCredential results in an "auth/user-mismatch" error when trying to use the Facebook credential with the anonymous user. While this error makes sense given the current user's status, I was hoping to receive an "auth/user-not-found" error if the credential does not exist - which unfortunately did not occur.
I am struggling to find a solution where I can allow an anonymous user to log in with Facebook and check if the provided credentials are linked to an existing user without creating a new account if one doesn't already exist.
The scenario driving this requirement is as follows: The system supports anonymous users;
- A user initially logs in anonymously and later converts to a registered user by linking their account with Facebook.
- If the app is uninstalled and then reinstalled,
- The user starting up the app will be anonymous once again.
- Upon attempting to log in using Facebook, the goal is to prevent the creation of a new user account if one is not already associated with the provided credentials. If a user ID already exists, the code should successfully transition from an anonymous account to the original user account.