0
0
Firebasecloud~10 mins

Phone number authentication in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Phone number authentication
User enters phone number
Send verification code
User receives code
User enters code
Verify code with Firebase
User logged in
The user inputs their phone number, receives a code, enters it, and Firebase verifies it to log them in or show an error.
Execution Sample
Firebase
const auth = getAuth();
const phoneNumber = '+1234567890';
const appVerifier = new RecaptchaVerifier('recaptcha-container', {}, auth);
signInWithPhoneNumber(auth, phoneNumber, appVerifier)
  .then(confirmationResult => {
    // User enters code here
  });
This code sends a verification code to the user's phone using Firebase Authentication.
Process Table
StepActionInput/ConditionFirebase ResponseNext Step
1User enters phone number+1234567890Phone number acceptedSend verification code
2Send verification codePhone number validCode sent to +1234567890Wait for user to enter code
3User enters codeUser inputs '123456'Code receivedVerify code
4Verify codeCode '123456' matchesVerification successUser logged in
5Verify codeCode '123456' does not matchVerification failedShow error message
6EndUser logged in or error shownProcess completeStop
💡 Process stops after successful login or error message shown due to failed verification.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
phoneNumberundefined+1234567890+1234567890+1234567890+1234567890+1234567890
verificationCodeundefinedundefinedundefined123456123456123456
verificationStatusundefinedundefinedundefinedpendingsuccess/failuresuccess/failure
userLoggedInfalsefalsefalsefalsetrue/falsetrue/false
Key Moments - 3 Insights
Why do we need to send a verification code after entering the phone number?
Sending the code confirms the phone number belongs to the user. See execution_table step 2 where Firebase sends the code after accepting the number.
What happens if the user enters the wrong code?
Firebase verification fails and an error message is shown. Refer to execution_table step 5 where verification fails and the process stops.
Why is the RecaptchaVerifier needed?
It prevents abuse by verifying the user is human before sending the code. This is part of Firebase's security, shown in the execution_sample code.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Firebase response after the user enters the phone number?
ACode sent to the phone number
BPhone number accepted
CVerification success
DVerification failed
💡 Hint
Check execution_table row 1 under 'Firebase Response'
At which step does the user enter the verification code?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at execution_table row 3 under 'Action'
If the verification code does not match, what is the next step?
AUser logged in
BSend verification code again
CShow error message
DUser enters phone number again
💡 Hint
See execution_table row 5 under 'Next Step'
Concept Snapshot
Phone number authentication with Firebase:
1. User enters phone number.
2. Firebase sends a verification code.
3. User inputs the code.
4. Firebase verifies the code.
5. On success, user logs in; on failure, error shown.
RecaptchaVerifier protects against abuse.
Full Transcript
Phone number authentication in Firebase starts when the user enters their phone number. Firebase sends a verification code to that number. The user then inputs the received code. Firebase checks if the code matches. If it does, the user is logged in. If not, an error message appears. A RecaptchaVerifier is used to ensure the request is from a real person. This process helps secure user login using their phone number.