Skip to content

Commit 8e85b2f

Browse files
committed
docs(auth): guard currentUser before linkWithCredential in Google examples
Avoid runtime errors when linking Google credentials while no user is signed in.
1 parent 0e3a310 commit 8e85b2f

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

docs/auth/social-auth.mdx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ with the new authentication state of the user.
496496
497497
To provide users with an additional login method, you can link their social media account (or an email & password) with their Firebase account.
498498
This is possible for any social provider that uses `signInWithCredential()`.
499-
To achieve this, you should replace sign-in method in any of the supported social sign-in code snippets with `getAuth().currentUser.linkWithCredential()`.
499+
To achieve this, you should replace the sign-in method in any of the supported social sign-in code snippets with `linkWithCredential()` on the signed-in user. Ensure `getAuth().currentUser` is not null before linking.
500500
501501
This code demonstrates linking a Google provider to an account that is already signed in using Firebase authentication.
502502
@@ -521,8 +521,13 @@ async function onGoogleLinkButtonPress() {
521521
throw new Error('Google Sign-In failed');
522522
}
523523

524+
const user = getAuth().currentUser;
525+
if (!user) {
526+
throw new Error('No user is signed in');
527+
}
528+
524529
const googleCredential = GoogleAuthProvider.credential(response.data.idToken);
525-
await getAuth().currentUser.linkWithCredential(googleCredential);
530+
await user.linkWithCredential(googleCredential);
526531
}
527532
```
528533
@@ -541,7 +546,12 @@ async function onGoogleLinkButtonPress() {
541546
throw new Error('No ID token found');
542547
}
543548

549+
const user = getAuth().currentUser;
550+
if (!user) {
551+
throw new Error('No user is signed in');
552+
}
553+
544554
const googleCredential = GoogleAuthProvider.credential(idToken);
545-
await getAuth().currentUser.linkWithCredential(googleCredential);
555+
await user.linkWithCredential(googleCredential);
546556
}
547557
```

0 commit comments

Comments
 (0)