I read that with Firebase I can allow users to sign in to my app using multiple authentication providers by linking auth provider credentials to an existing user account. Is possible linking custom auth provider such as Linkedin? I read that I need to pass the AuthCredential object to the signed-in user's linkWithCredential method but I don't find a custom AuthCredential.
One way to link an unsupported provider custom token to an existing account is to get the Firebase account's user id and the unsupported provider user id and save a hash map that takes in the unsupported provider id and returns the firebase uid which you want to link to. When the user signs in to unsupported provider with custom token, you get the corresponding firebase uid from the map and return a custom token with that uid which on signInWithCustomToken resolves with the original firebase user.
The downside is that you don't see the unsupported provider in the list of provider data within the user. You also need to store the map.
I would like to expand bojeil's answer.
There is firebaseUser.linkWithCredential(credential)
for supported Providers, but not equivalent for customProvider. Linking customProvider must be done by backend (or maybe sth like Firebase Functions). Because link
means one of these:
- add email
- add phone number
- custom logic (add user claim)
to the Firebase User
object.
Flow is like this:
- Client gets
email
,phone
or some unique info from customProvider (Line, LinkedIn, Huawei...) and send them to Backend includingfirebaseToken
. - (Backend may verify those data by asking to customProvider). Then Backend adds those info to
FirebaseUser
object. (Also backend has to check if those data are already attached to some other user. You might reject linking, since there is another user already with these email, phone...) - Backend saves
<CustomProviderId-FirebaseUserId>
pair somewhere (e.g:FireStore
). This is done because in future, when user wants to sign in using customProvider, backends needs to createCustomToken
(so client can callfirebaseAuth.signInWithCustomToken
) usingFirebase Id
of that user. So this Mapping is solution for that problem. - Backend will respond with 200 HTTP response code to indicate linking is successful
- Client calls
firebaseUser.reload()
to get newly attached data (email, phone etc...) - If User Claims are updated in step-2, then client also has to call
firebaseUser.getIdToken(force=true)
to get the updated user claims.
There are challenges:
- If customProvider only gives
email
, you need to check if that email is attached to any other user already. - If customProvider only gives
phone
, you need to check if that phone is attached to any other user already. - If customProvider can give both
email
andphone
, complexity increases, as you have to check if any of those already attached to any other user. - If customProvider does not provide
email
orphone
, then you need unique info for that user for that custom provider (such as Huawei User Id). - CustomProvider will not be in
firebaseUser.providerData
list, so you might add user claims. (e.g.{kakaoTalk:true, huawei:true}
). Depending on case, you don't need that. For example, if customProvider is WhatsApp, then ifphone
exists infirebaseUser.phoneNumber
it means WhatsApp is linked (even phone is added using another sign in provider). - If you allow
Anonymous
users, then linking customProvider may not updatefirebaseUser.isAnonymous
on client if email/phone is not updated, client will still see firebaseUser as anonymous. One solution is to check if linkedProviders are empty to consider user as anonymous, if that is ok with your business logic. Other solution is Anonymous user status changes when you sign in user usingsignInWithCustomToken
. So after linking customProvider is successful, silently logout and sign in with customToken so user becomes not anonymous. - If you link customProvider with
email
to anonymous user, that user'semail
field is updated. This will force client to logout and throwFirebaseAuthInvalidUserException
with errorCodeERROR_USER_TOKEN_EXPIRED
. User has to sign in again.
© 2022 - 2024 — McMap. All rights reserved.
signInWithCustomToken( newToken )
. These two users merged. – Coolie