Phone number authentication in Firebase - Time & Space Complexity
We want to understand how the time needed to verify phone numbers changes as more users try to sign in.
How does the system handle more requests and how does that affect speed?
Analyze the time complexity of the following operation sequence.
const phoneNumber = "+1234567890";
const appVerifier = window.recaptchaVerifier;
firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
.then(confirmationResult => {
// User receives SMS code
const code = prompt('Enter the verification code');
return confirmationResult.confirm(code);
})
.then(userCredential => {
// User signed in
})
.catch(error => {
// Handle errors
});
This code sends a verification SMS to the user and then confirms the code they enter to sign them in.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Sending SMS verification via Firebase API.
- How many times: Once per user sign-in attempt.
Each new user triggers one SMS send and one verification check.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 SMS sends + 10 code verifications |
| 100 | 100 SMS sends + 100 code verifications |
| 1000 | 1000 SMS sends + 1000 code verifications |
Pattern observation: The number of operations grows directly with the number of users trying to sign in.
Time Complexity: O(n)
This means the time needed grows linearly as more users try to authenticate with their phone numbers.
[X] Wrong: "Sending one SMS code will handle multiple users at once, so time stays the same."
[OK] Correct: Each user needs their own SMS and verification, so the system must do separate work for each user.
Understanding how authentication scales helps you design systems that stay fast and reliable as more people use them.
"What if we batch phone number verifications instead of sending SMS one by one? How would the time complexity change?"
Practice
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 AQuick Check:
Phone number authentication = SMS sign-in [OK]
- Confusing phone auth with email verification
- Thinking it stores phone numbers only
- Assuming it sends marketing SMS
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 BQuick Check:
Send SMS code = signInWithPhoneNumber() [OK]
- Using email sign-in methods for phone auth
- Confusing password reset with phone auth
- Assuming verifyPhoneNumber() is a Firebase method
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 DQuick Check:
Missing reCAPTCHA = error thrown [OK]
- Assuming SMS sends without reCAPTCHA
- Thinking user signs in automatically
- Believing phone number stores without SMS
const confirmationResult = await signInWithPhoneNumber(auth, phoneNumber, appVerifier); const userCredential = await confirmationResult.confirm(code);
Solution
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 CQuick Check:
Missing appVerifier init = error [OK]
- Confusing confirm() with verify() method
- Passing email instead of phone number
- Assuming code must be number type
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 AQuick Check:
reCAPTCHA -> send SMS -> confirm code = correct flow [OK]
- Calling signInWithPhoneNumber before reCAPTCHA setup
- Confirming code before SMS sent
- Trying to sign in before confirmation
Ready to prove it?
Pass the test and this pattern moves unknown
