What if you could give users just the right access without endless code and mistakes?
Why Custom authentication tokens in Firebase? - Purpose & Use Cases
Imagine you have a website where users log in, but you want to give some users special access based on their roles or external system data. You try to manage all these permissions by hand, checking user info every time they do something.
Doing this manually means writing lots of repeated code everywhere. It's easy to make mistakes, forget to check permissions, or expose sensitive data. Also, updating user roles or syncing with other systems becomes a slow, confusing mess.
Custom authentication tokens let you create secure, trusted tokens that include exactly the user info and permissions you want. These tokens are generated once and then used safely by your app, making user access smooth and reliable.
if (user.role === 'admin') { allowAccess(); } else { denyAccess(); }
const token = createCustomToken(userId, { role: 'admin' });You can securely control user access and integrate external data into your app's login system without messy, repeated checks.
A company uses custom tokens to let employees log in with their company ID and automatically get access to the right internal tools based on their department.
Manual permission checks are slow and error-prone.
Custom tokens package user info securely for easy access control.
This makes apps safer and simpler to manage.
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
