icdeverything
icdeverything

Reputation: 51

How to use Phone verification without creating new user

Hi I'm using react native now. I've been facing this problem

I want to use Phone verification without creating new user.

But I can't find what I want to. I tried using npm @react-native-firebase/auth and firebase auth I couldn't find it. I'd really appreciate your help.

Upvotes: 0

Views: 811

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

I want to use Phone verification without create new user.

Phone number verification is built into Firebase as a way of creating/verifying a user. These is no way to use Firebase's phone number verification without creating a user account in Firebase Authentication.

Upvotes: 2

aelarassi
aelarassi

Reputation: 121

Firebase provides support for locally testing phone numbers. For local testing to work, you must have ensured your local machine SHA1 debug key was added whilst creating your application on the Firebase Console.

After successfully creating a user with an email and password (see Authentication/Usage/Email/Password sign-in), use the verifyPhoneNumber method to send a verification code to a user's phone number and if the user enters the correct code, link the phone number to the authenticated user's account.

{...}
import auth from '@react-native-firebase/auth';

export default function PhoneVerification() {
     {...}

     // Handle the verify phone button press
       async function verifyPhoneNumber(phoneNumber) {
         const confirmation = await auth().verifyPhoneNumber(phoneNumber);
         console.log(confirmation);
       }

     // Handle confirm code button press
       async function confirmCode() {
         try {
           const credential = auth.PhoneAuthProvider.credential(confirm.verificationId, code);
           let userData = await auth().currentUser.linkWithCredential(credential);
           setUser(userData.user);
         } catch (error) {
           if (error.code == 'auth/invalid-verification-code') {
             console.log('Invalid code.');
           } else {
             console.log('Account linking error');
           }
         }
       }


     {...}

     <Button
      title="Verify Phone Number"
      onPress={() => verifyPhoneNumber('ENTER A VALID TESTING OR REAL PHONE NUMBER HERE')}
    />


     <Button title="Confirm Code" onPress={() => confirmCode()} />

     {...}
}

Enter phone number (e.g. +44 7444 555666) and a test code (e.g. 123456). For more datails https://rnfirebase.io/auth/phone-auth

Upvotes: 1

Related Questions