Skip to content

Commit 918f0dd

Browse files
authored
docs(auth): Link Multiple Auth Providers to a Firebase Account (#7479)
1 parent 1318eaf commit 918f0dd

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

docs/auth/social-auth.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,34 @@ Additionally, the similar `linkWithRedirect` and `linkWithPopup` methods may be
380380

381381
Upon successful sign-in, any [`onAuthStateChanged`](/auth/usage#listening-to-authentication-state) listeners will trigger
382382
with the new authentication state of the user.
383+
384+
## Link Multiple Auth Providers to a Firebase Account
385+
386+
[From the official documentation:](https://firebase.google.com/docs/auth/web/google-signin#expandable-1)
387+
388+
> If you enabled the **One account per email address** setting in the Firebase console, when a user tries to sign in a to a provider (such as Google) with an email that already exists for another Firebase user's provider (such as Facebook), the error `auth/account-exists-with-different-credential` is thrown along with an `AuthCredential` object (Google ID token). To complete the sign in to the intended provider, the user has to sign first to the existing provider (Facebook) and then link to the former `AuthCredential` (Google ID token).
389+
390+
To provide users with an additional login method, you can link their social media account (or an email & password) with their Firebase account. This is possible for any social provider that uses `auth().signInWithCredential()`.
391+
To achieve this, you should replace sign-in method in any of the supported social sign-in code snippets with `auth().currentUser.linkWithCredential()`.
392+
393+
This code demonstrates linking a Google provider to an account that is already signed in using Firebase authentication.
394+
395+
```js
396+
import auth from '@react-native-firebase/auth';
397+
import { GoogleSignin } from '@react-native-google-signin/google-signin';
398+
399+
async function onGoogleLinkButtonPress() {
400+
// Ensure the device supports Google Play services
401+
await GoogleSignin.hasPlayServices({ showPlayServicesUpdateDialog: true });
402+
// Obtain the user's ID token
403+
const { idToken } = await GoogleSignin.signIn();
404+
405+
// Create a Google credential with the token
406+
const googleCredential = auth.GoogleAuthProvider.credential(idToken);
407+
408+
// Link the user's account with the Google credential
409+
const firebaseUserCredential = await auth().currentUser.linkWithCredential(googleCredential);
410+
// Handle the linked account as needed in your app
411+
return;
412+
}
413+
```

0 commit comments

Comments
 (0)