Custom authentication tokens in Firebase - Time & Space Complexity
When using custom authentication tokens in Firebase, it's important to understand how the time to create and verify these tokens changes as you handle more users.
We want to know: how does the work grow when we generate tokens for many users?
Analyze the time complexity of the following operation sequence.
const admin = require('firebase-admin');
async function createCustomTokens(userIds) {
const tokens = [];
for (const uid of userIds) {
const token = await admin.auth().createCustomToken(uid);
tokens.push(token);
}
return tokens;
}
This code creates a custom authentication token for each user ID in a list.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Calling
createCustomToken(uid)for each user. - How many times: Once per user ID in the input list.
Each user requires one token creation call, so the total work grows directly with the number of users.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 token creations |
| 100 | 100 token creations |
| 1000 | 1000 token creations |
Pattern observation: The number of operations increases evenly as the input size grows.
Time Complexity: O(n)
This means the time to create tokens grows in direct proportion to the number of users.
[X] Wrong: "Creating one token is slow, so creating many tokens will take the same time as one."
[OK] Correct: Each token creation is a separate operation, so more users mean more work and more time.
Understanding how your code scales with more users shows you can build systems that handle growth smoothly and predict performance.
"What if we batch user IDs and create tokens in parallel? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of custom tokens
Custom tokens are used to authenticate users from an external system with Firebase.Step 2: Identify the correct purpose
They allow users to sign in securely using tokens generated on your server.Final Answer:
To sign in users from your own system to Firebase -> Option BQuick Check:
Custom tokens = sign in users [OK]
- Confusing tokens with data storage
- Thinking tokens send notifications
- Assuming tokens create projects
Solution
Step 1: Identify the correct Admin SDK service
Custom tokens are created using the Authentication service, not database or storage.Step 2: Match the correct method
The method to create a custom token is createCustomToken(uid) under admin.auth().Final Answer:
admin.auth().createCustomToken(uid) -> Option AQuick Check:
Custom token creation = admin.auth().createCustomToken [OK]
- Using database or storage methods incorrectly
- Confusing signInWithCustomToken with token creation
- Calling methods on firebase instead of admin SDK
uid is 'user123' and no errors occur?
const admin = require('firebase-admin');
admin.initializeApp();
admin.auth().createCustomToken('user123')
.then(token => console.log(typeof token))
.catch(error => console.error(error));Solution
Step 1: Understand createCustomToken return type
The method returns a Promise that resolves to a string token.Step 2: Check console.log output
Logging typeof token will print 'string' because the token is a string.Final Answer:
'string' -> Option DQuick Check:
createCustomToken returns string token [OK]
- Expecting an object instead of string
- Confusing token with undefined
- Assuming an error without cause
const admin = require('firebase-admin');
admin.auth().createCustomToken('12345')
.then(token => console.log(token))
.catch(error => console.error(error));Solution
Step 1: Check initialization of Firebase Admin SDK
The admin SDK must be initialized with admin.initializeApp() before use.Step 2: Verify other parts
The UID '12345' is a valid string, createCustomToken returns a Promise, and method name is correct.Final Answer:
Missing admin.initializeApp() call -> Option AQuick Check:
Initialize admin SDK before calling methods [OK]
- Passing number instead of string UID
- Assuming createCustomToken is synchronous
- Using wrong method name
role: 'admin' in a custom token. Which code snippet correctly adds this claim?Solution
Step 1: Understand how to add extra claims in custom tokens
The createCustomToken method accepts a second argument with additional claims as a plain object.Step 2: Evaluate each option
admin.auth().createCustomToken(uid, { role: 'admin' }) correctly passes extra claims as second argument. admin.auth().createCustomToken(uid).setClaims({ role: 'admin' }) is invalid chaining. admin.auth().setCustomUserClaims(uid, { role: 'admin' }) sets claims on user but not in token creation. admin.auth().createCustomToken(uid, { claims: { role: 'admin' } }) nests claims incorrectly.Final Answer:
admin.auth().createCustomToken(uid, { role: 'admin' }) -> Option CQuick Check:
Extra claims go as second argument in createCustomToken [OK]
- Trying to chain setClaims on token creation
- Using setCustomUserClaims instead of token claims
- Nesting claims inside another object
Ready to prove it?
Pass the test and this pattern moves unknown
