-
Notifications
You must be signed in to change notification settings - Fork 256
Add v8 and v9 snippets for Handling the account-exists-with-different… #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
snippets/auth-next/link-multiple-accounts/account_exists_popup.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// This snippet file was generated by processing the source file: | ||
// ./auth-next/link-multiple-accounts.js | ||
// | ||
// To update the snippets in this file, edit the source and then run | ||
// 'npm run snippets'. | ||
|
||
// [START account_exists_popup_modular] | ||
import { signInWithPopup, signInWithEmailAndPassword, linkWithCredential } from "firebase/auth"; | ||
|
||
// User tries to sign in with Facebook. | ||
signInWithPopup(auth, facebookProvider).catch((error) => { | ||
// User's email already exists. | ||
if (error.code === 'auth/account-exists-with-different-credential') { | ||
// The pending Facebook credential. | ||
const pendingCred = error.credential; | ||
// The provider account's email address. | ||
const email = error.customData.email; | ||
|
||
// Present the user with a list of providers they might have | ||
// used to create the original account. | ||
// Then, ask the user to sign in with the existing provider. | ||
const method = promptUserForSignInMethod(); | ||
|
||
if (method === 'password') { | ||
// TODO: Ask the user for their password. | ||
// In real scenario, you should handle this asynchronously. | ||
const password = promptUserForPassword(); | ||
signInWithEmailAndPassword(auth, email, password).then((result) => { | ||
return linkWithCredential(result.user, pendingCred); | ||
}).then(() => { | ||
// Facebook account successfully linked to the existing user. | ||
goToApp(); | ||
}); | ||
return; | ||
} | ||
|
||
// All other cases are external providers. | ||
// Construct provider object for that provider. | ||
// TODO: Implement getProviderForProviderId. | ||
const provider = getProviderForProviderId(method); | ||
// At this point, you should let the user know that they already have an | ||
// account with a different provider, and validate they want to sign in | ||
// with the new provider. | ||
// Note: Browsers usually block popups triggered asynchronously, so in | ||
// real app, you should ask the user to click on a "Continue" button | ||
// that will trigger signInWithPopup(). | ||
signInWithPopup(auth, provider).then((result) => { | ||
// Note: Identity Platform doesn't control the provider's sign-in | ||
// flow, so it's possible for the user to sign in with an account | ||
// with a different email from the first one. | ||
|
||
// Link the Facebook credential. We have access to the pending | ||
// credential, so we can directly call the link method. | ||
linkWithCredential(result.user, pendingCred).then((userCred) => { | ||
// Success. | ||
goToApp(); | ||
}); | ||
}); | ||
} | ||
}); | ||
// [END account_exists_popup_modular] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was from the existing snippet. I think this is to tell developers to implement
promptUserForPassword
so let's keep it.