Password reset flow in Firebase - Time & Space Complexity
When users reset their passwords using Firebase, the system makes calls to send reset emails and verify tokens.
We want to understand how the number of these calls grows as more users request password resets.
Analyze the time complexity of the following operation sequence.
const auth = getAuth();
function sendPasswordReset(email) {
return sendPasswordResetEmail(auth, email);
}
// Later, user clicks link and token is verified
function verifyResetCode(code) {
return verifyPasswordResetCode(auth, code);
}
This sequence sends a password reset email and then verifies the reset code when the user clicks the link.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Sending password reset emails via
sendPasswordResetEmail. - How many times: Once per user request for password reset.
- Secondary operation: Verifying reset codes via
verifyPasswordResetCode, once per user clicking the reset link.
Each password reset request triggers one email send operation.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 email sends |
| 100 | 100 email sends |
| 1000 | 1000 email sends |
Pattern observation: The number of email sends grows directly with the number of reset requests.
Time Complexity: O(n)
This means the time and resources needed grow in direct proportion to the number of password reset requests.
[X] Wrong: "Sending one password reset email handles multiple users at once, so time stays the same regardless of requests."
[OK] Correct: Each user needs their own reset email, so the system must send one email per request, increasing work as requests grow.
Understanding how user-triggered operations scale helps you design systems that handle growth smoothly and predict resource needs.
"What if the system batched password reset emails to send them all at once? How would the time complexity change?"
Practice
sendPasswordResetEmail do?Solution
Step 1: Understand the function purpose
The functionsendPasswordResetEmailis designed to send an email to the user with a link to reset their password.Step 2: Differentiate from other actions
This function does not change the password directly or affect the account status; it only initiates the reset process by sending an email.Final Answer:
Sends an email to the user to reset their password -> Option AQuick Check:
sendPasswordResetEmail = Sends reset email [OK]
- Thinking it changes the password immediately
- Confusing it with account deletion
- Assuming it creates a new user
Solution
Step 1: Recall the correct Firebase method
The correct method to send a password reset email issendPasswordResetEmailcalled on the auth object.Step 2: Check method call syntax
The auth object is accessed byfirebase.auth(), so the full call isfirebase.auth().sendPasswordResetEmail(email).Final Answer:
firebase.auth().sendPasswordResetEmail(email) -> Option AQuick Check:
Correct method call syntax = firebase.auth().sendPasswordResetEmail(email) [OK]
- Omitting parentheses after auth
- Using wrong method names like sendPasswordReset
- Swapping method words order
firebase.auth().sendPasswordResetEmail('user@example.com')
.then(() => console.log('Email sent'))
.catch(error => console.log(error.message));
What will be printed if the email is successfully sent?Solution
Step 1: Analyze the promise resolution
ThesendPasswordResetEmailreturns a promise that resolves when the email is sent successfully.Step 2: Check the then and catch handlers
On success, thethencallback logs 'Email sent'. On failure, thecatchlogs the error message.Final Answer:
Email sent -> Option DQuick Check:
Success logs 'Email sent' [OK]
- Confusing catch output with success
- Expecting no output on success
- Misreading promise behavior
firebase.auth.sendPasswordResetEmail('user@example.com')
What is the likely error?Solution
Step 1: Check method call syntax
The code callsfirebase.auth.sendPasswordResetEmailwithout parentheses afterauth, which is incorrect.Step 2: Understand correct usage
The correct call isfirebase.auth().sendPasswordResetEmail(email)becauseauthis a function that returns the auth object.Final Answer:
Missing parentheses after auth() -> Option BQuick Check:
auth() must be called as a function [OK]
- Omitting parentheses after auth
- Assuming method names are wrong
- Ignoring syntax errors
Solution
Step 1: Understand promise handlers order
Thethenhandler runs on success, andcatchruns on failure.Step 2: Match messages to handlers
Success should show 'Reset email sent', failure should show 'Failed to send email'. Only firebase.auth().sendPasswordResetEmail(email) .then(() => alert('Reset email sent')) .catch(() => alert('Failed to send email')) matches this logic correctly.Final Answer:
firebase.auth().sendPasswordResetEmail(email) .then(() => alert('Reset email sent')) .catch(() => alert('Failed to send email')) -> Option CQuick Check:
then() = success, catch() = error [OK]
- Swapping then and catch handlers
- Using finally for success message
- Showing error message on success
Ready to prove it?
Pass the test and this pattern moves unknown
