I'm creating a web app in React with firebase services. I have Google and Facebook login on the login screen and after login user gets an option to link their phone. I use Firebase Phone Auth for this. The user is already signed then they Auth using the phone. I want to Link Phone Auth user object with facebook/google account.
Going through the docs I was not able to find the solution that suits my use case. Help will be appreciated.
Create fictional phone numbers and verification codesIn the Firebase console, open the Authentication section. In the Sign in method tab, enable the Phone provider if you haven't already. Open the Phone numbers for testing accordion menu. Provide the phone number you want to test, for example: +1 650-555-3434.
Here is a simplified example how to link a phone number to a Google/Facebook user using an invisible reCAPTCHA.
// Sign in the Google user first.
firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider())
.then(function(result) {
// Google user signed in. Check if phone number added.
if (!result.user.phoneNumber) {
// Ask user for phone number.
var phoneNumber = window.prompt('Provide your phone number');
// You also need to provide a button element signInButtonElement
// which the user would click to complete sign-in.
// Get recaptcha token. Let's use invisible recaptcha and hook to the button.
var appVerifier = new firebase.auth.RecaptchaVerifier(
signInButtonElement, {size: 'invisible'});
// This will wait for the button to be clicked the reCAPTCHA resolved.
return result.user.linkWithPhoneNumber(phoneNumber, appVerifier)
.then(function(confirmationResult) {
// Ask user to provide the SMS code.
var code = window.prompt('Provide your SMS code');
// Complete sign-in.
return confirmationResult.confirm(code);
})
}
})
.catch(function(error) {
// console.log(error);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With