Link an authentication provider already used for sign in

1,269 views
Skip to first unread message

Arnaud Deman

unread,
Aug 11, 2018, 3:27:44 PM8/11/18
to Firebase Google Group
Hi,

I am using the method linkWithPopup to link the current user to a given provider.
If the user has already used this provider to sign in, it fails with the error code 'auth/credential-already-in-use'.

To handle this case, I would like to use the same approach as explained in the documentation for email and passord: merge the accounts, delete the unwanted one and link with the provider.

For this, I would need the credential but as the method linkWithPopup failled I don't know how to get it without asking the user to sign in with the associated provider. 

I would like all this to be as transparent as possible for the final user and I wonder what would be the good approach for this.

Thanks in advance for your advices,
Regards,
Arnaud.




Bassam

unread,
Aug 11, 2018, 5:20:25 PM8/11/18
to Firebase Google Group
Hey Arnaud,
The error will contain the credential.

firebase.auth().currentUser.linkWithPopup(provider)
  .catch(function(error) {
     if (error.code == 'auth/credential-already-in-use') {
       // You can get the email from error.email and the credential from error.credential.
       // In this case, you can directly sign in with that credential without having to call signInWithPopup again.
       // One option here is to initialize another FirebaseApp, sign in with this credential,
       // merge both users and then sign in again in the original auth instance after both users are merged.
     } else {
       ...
     }
  });

Best regards,
Bassam

Arnaud Deman

unread,
Aug 13, 2018, 1:56:18 AM8/13/18
to Firebase Google Group
Hi Bassam,

Thanks for your answer.
Regardings the doc, your solution should work... But I still can't have access to the the credential. I think I have missed something but I can't find what.

This is the code I use and the console output:

this._firebaseAuth.auth.currentUser.linkWithPopup(this._fetchFirebaseAuthProvider(authenticationProviderVO))
.then(res => res)
.catch(error => {
if (error.code === AUTHENTICATION_ERRORS.CREDENTIAL_ALREADY_USED) {
this.logger.debug('linkWithProvider Failure, code and credential', error.code, error.email, error.credential);
throw error;
}
})

Console output:
DEBUG - AuthenticationProviderService - 2018/07/00 11:26:03 - linkWithProvider Failure, code and credential auth/credential-already-in-use undefined undefined

It works when the account does not exist in firebase. I tried with Google and Twitter providers. 
Thanks for your help.

Regards,
Arnaud.

Bassam Ojeil

unread,
Aug 13, 2018, 2:22:06 AM8/13/18
to fireba...@googlegroups.com
I tested linkWithPopup using an existing Google account. I got the expected error with the email and credential.
Can you output the entire content of the error?
It may be possible that error.credential and error.email may be getting obfuscated when you compile your app. Try using error['credential'] instead.

Bassam

--
You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/olkMXVmxXrk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/8511683c-ffbe-4199-b74b-910051f3f60a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Arnaud Deman

unread,
Aug 13, 2018, 12:57:37 PM8/13/18
to Firebase Google Group
Hi,
I tried to sign in in with email/password and then link with google provider.
I removed all specific code :
this._firebaseAuth.auth.currentUser.linkWithPopup(new firebase.auth.GoogleAuthProvider())
.then(res => res)
.catch(error => {
if (error.code === 'auth/credential-already-in-use') {
console.log('linkWithProvider Failure, error', error);
console.log('linkWithProvider Failure, error["credential"]', error["credential"]);
throw error;
}
})

The console output:
linkWithProvider Failure, error L {code: "auth/credential-already-in-use", message: "This credential is already associated with a different user account."}
linkWithProvider Failure, error["credential"] undefined

I use Angular 6 and the versions associated to firebase in my package.json are:
"angularfire2": "^5.0.0-rc.11",
"firebase": "^5.3.1",

Maybe it's more of an Angularfire2 problem if I have not missed something.

Thanks for your help,
Arnaud.

Bassam

unread,
Aug 13, 2018, 3:53:01 PM8/13/18
to Firebase Google Group
Hey Arnaud,
I honestly have no idea why it's not there. I personally can't recreate it with the core SDK.
Can you try using the core SDK directly to confirm whether the root cause of the issue is in angularfire2?

Bassam

Arnaud Deman

unread,
Aug 14, 2018, 9:31:32 AM8/14/18
to Firebase Google Group
Hey Bassam, 

As you suggested I tried with the core SDK and the problem occures when the firebase authentication is configured to allow mutliple accounts per email before the user is registered.

I would say it is a bug, but perhaps there is a good reason for this behaviour ?

Thanks,
Arnaud.

Bassam

unread,
Aug 14, 2018, 1:52:25 PM8/14/18
to Firebase Google Group
Now it makes sense. I was not able to replicate this because I was testing the default mode.
I think we should fix this. I will file an internal bug to do so. Feel free to file an issue externally via support so you can track it: https://firebase.google.com/support/
Thanks for reporting the issue.

Bassam
Reply all
Reply to author
Forward
0 new messages