📖 Scenario: You are building a simple app that lets users sign in using their phone numbers. This method sends a code to the user's phone, which they enter to verify their identity.
🎯 Goal: Create a Firebase phone number authentication flow that sends a verification code to the user's phone and verifies it.
📋 What You'll Learn
Initialize Firebase app with config
Set up reCAPTCHA verifier
Send verification code to a phone number
Verify the code entered by the user
💡 Why This Matters
🌍 Real World
Phone number authentication is widely used in apps for easy and secure user sign-in without passwords.
💼 Career
Understanding Firebase phone authentication is valuable for building secure mobile and web apps in many development roles.
Progress0 / 4 steps
1
Initialize Firebase App
Create a variable called firebaseConfig with the exact keys: apiKey, authDomain, projectId, storageBucket, messagingSenderId, and appId with these exact string values: "your-api-key", "your-auth-domain", "your-project-id", "your-storage-bucket", "your-messaging-sender-id", "your-app-id". Then initialize Firebase app using initializeApp(firebaseConfig) and assign it to app.
Firebase
Hint
Use const firebaseConfig = { ... } with the exact keys and values. Then call initializeApp(firebaseConfig) and assign it to app.
2
Set Up reCAPTCHA Verifier
Create a variable called auth by calling getAuth(app). Then create a variable called recaptchaVerifier by calling new RecaptchaVerifier('recaptcha-container', { size: 'invisible' }, auth).
Firebase
Hint
Use getAuth(app) to get the auth object. Then create recaptchaVerifier with new RecaptchaVerifier('recaptcha-container', { size: 'invisible' }, auth).
3
Send Verification Code
Create a function called sendVerificationCode that takes a parameter phoneNumber. Inside it, call signInWithPhoneNumber(auth, phoneNumber, recaptchaVerifier) and assign the result to a variable called confirmationResult. Return confirmationResult.
Firebase
Hint
Define an async function sendVerificationCode(phoneNumber). Use await signInWithPhoneNumber(auth, phoneNumber, recaptchaVerifier) and return the result.
4
Verify the Code Entered by User
Create an async function called verifyCode that takes parameters confirmationResult and code. Inside it, call confirmationResult.confirm(code) and assign the result to userCredential. Return userCredential.
Firebase
Hint
Define an async function verifyCode(confirmationResult, code). Use await confirmationResult.confirm(code) and return the result.
Practice
(1/5)
1. What is the main purpose of phone number authentication in Firebase?
easy
A. To sign in users using SMS codes without passwords
B. To store phone numbers in the database
C. To send marketing messages to users
D. To verify email addresses
Solution
Step 1: Understand and identify the main purpose of phone number authentication
It allows users to sign in by receiving a code on their phone via SMS instead of using a password. The main goal is to authenticate users securely and easily using their phone number and SMS code.
Final Answer:
To sign in users using SMS codes without passwords -> Option A
Quick Check:
Phone number authentication = SMS sign-in [OK]
Hint: Phone auth uses SMS codes to sign in, not emails [OK]
Common Mistakes:
Confusing phone auth with email verification
Thinking it stores phone numbers only
Assuming it sends marketing SMS
2. Which Firebase method is used to start phone number authentication by sending an SMS code?
easy
A. signInWithEmailAndPassword()
B. signInWithPhoneNumber()
C. sendPasswordResetEmail()
D. verifyPhoneNumber()
Solution
Step 1: Identify the method for phone auth SMS sending and exclude unrelated methods
Firebase provides signInWithPhoneNumber() to send the SMS code to the user. signInWithEmailAndPassword() is for email login, sendPasswordResetEmail() is for password reset, and verifyPhoneNumber() is not a Firebase client method.
Final Answer:
signInWithPhoneNumber() -> Option B
Quick Check:
Send SMS code = signInWithPhoneNumber() [OK]
Hint: Phone auth starts with signInWithPhoneNumber() [OK]
Common Mistakes:
Using email sign-in methods for phone auth
Confusing password reset with phone auth
Assuming verifyPhoneNumber() is a Firebase method
3. What will happen if you try to sign in with phone number authentication without setting up reCAPTCHA verifier in Firebase web?
medium
A. The phone number will be stored but no SMS sent
B. The SMS code will be sent without any issues
C. The user will be signed in automatically
D. Firebase will throw an error requiring reCAPTCHA verification
Solution
Step 1: Understand reCAPTCHA role and predict behavior without it in Firebase phone auth
Firebase requires reCAPTCHA on web to prevent abuse and verify the user is human before sending SMS. If reCAPTCHA is not set up, Firebase will block the SMS sending and throw an error demanding reCAPTCHA verification.
Final Answer:
Firebase will throw an error requiring reCAPTCHA verification -> Option D
Quick Check:
Missing reCAPTCHA = error thrown [OK]
Hint: Web phone auth needs reCAPTCHA or it errors [OK]
Step 1: Check parameters and identify common mistake in signInWithPhoneNumber code
The third parameter must be a valid reCAPTCHA verifier instance (appVerifier) initialized properly. If appVerifier is not initialized or missing, the code will fail even if confirm() is called correctly.
Final Answer:
Missing reCAPTCHA verifier initialization -> Option C
Quick Check:
Missing appVerifier init = error [OK]
Hint: Always initialize reCAPTCHA verifier before signInWithPhoneNumber() [OK]
Common Mistakes:
Confusing confirm() with verify() method
Passing email instead of phone number
Assuming code must be number type
5. You want to implement phone number authentication in a React app using Firebase. Which sequence correctly describes the steps to sign in a user?
hard
A. Initialize reCAPTCHA verifier -> call signInWithPhoneNumber() -> send SMS -> confirm code -> sign in user
B. Call signInWithPhoneNumber() -> confirm code -> initialize reCAPTCHA verifier -> sign in user
C. Confirm code -> initialize reCAPTCHA verifier -> call signInWithPhoneNumber() -> sign in user
D. Sign in user -> initialize reCAPTCHA verifier -> call signInWithPhoneNumber() -> confirm code
Solution
Step 1: Outline the correct sequence for implementing phone auth
Initialize reCAPTCHA verifier first to prevent abuse and required before sending SMS. Call signInWithPhoneNumber() next to send the code to the user's phone. Confirm the code received by user using confirmationResult.confirm(code) to verify and sign in the user.
Final Answer:
Initialize reCAPTCHA verifier -> call signInWithPhoneNumber() -> send SMS -> confirm code -> sign in user -> Option A