Bird
Raised Fist0
Firebasecloud~10 mins

Custom authentication tokens in Firebase - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a custom token with Firebase Admin SDK.

Firebase
const admin = require('firebase-admin');

admin.initializeApp();

const uid = 'some-uid';
admin.auth().[1](uid)
  .then((customToken) => {
    console.log(customToken);
  })
  .catch((error) => {
    console.log('Error creating custom token:', error);
  });
Drag options to blanks, or click blank then click option'
AcreateCustomToken
BgenerateToken
CsignInWithCustomToken
DgetCustomToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using generateToken instead of createCustomToken
Trying to use client SDK methods like signInWithCustomToken on the server
2fill in blank
medium

Complete the code to add custom claims to the token.

Firebase
const uid = 'user123';
const additionalClaims = { admin: true };

admin.auth().createCustomToken(uid, [1])
  .then((token) => {
    console.log(token);
  });
Drag options to blanks, or click blank then click option'
AextraClaims
BadditionalClaims
CcustomClaims
Dclaims
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the claims object with a wrong variable name
Not passing any claims object when needed
3fill in blank
hard

Fix the error in the code to verify a custom token on the client side.

Firebase
firebase.auth().[1](customToken)
  .then((userCredential) => {
    console.log('User signed in');
  })
  .catch((error) => {
    console.error(error);
  });
Drag options to blanks, or click blank then click option'
AauthenticateWithToken
BverifyIdToken
CcreateCustomToken
DsignInWithCustomToken
Attempts:
3 left
💡 Hint
Common Mistakes
Using verifyIdToken which is a server-side method
Using createCustomToken on the client side
4fill in blank
hard

Fill both blanks to decode and verify a custom token on the server.

Firebase
admin.auth().[1](idToken)
  .then((decodedToken) => {
    const uid = decodedToken.[2];
    console.log('User ID:', uid);
  })
  .catch((error) => {
    console.error('Error verifying token:', error);
  });
Drag options to blanks, or click blank then click option'
AverifyIdToken
BdecodeToken
Cuid
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using decodeToken which is not a Firebase Admin method
Accessing userId instead of uid
5fill in blank
hard

Fill all three blanks to create a custom token with claims and handle errors properly.

Firebase
const uid = 'user456';
const additionalClaims = { premiumAccount: true };

admin.auth().[1](uid, [2])
  .then((token) => {
    console.log('Token:', token);
  })
  .catch(([3]) => {
    console.error('Failed to create token:', error);
  });
Drag options to blanks, or click blank then click option'
AcreateCustomToken
Bclaims
Cerror
DadditionalClaims
Attempts:
3 left
💡 Hint
Common Mistakes
Using claims instead of additionalClaims as variable name
Not naming the catch parameter error

Practice

(1/5)
1. What is the main purpose of Firebase custom authentication tokens?
easy
A. To store user data in Firebase Realtime Database
B. To sign in users from your own system to Firebase
C. To send notifications to users
D. To create Firebase projects automatically

Solution

  1. Step 1: Understand the role of custom tokens

    Custom tokens are used to authenticate users from an external system with Firebase.
  2. Step 2: Identify the correct purpose

    They allow users to sign in securely using tokens generated on your server.
  3. Final Answer:

    To sign in users from your own system to Firebase -> Option B
  4. Quick Check:

    Custom tokens = sign in users [OK]
Hint: Custom tokens = external user sign-in method [OK]
Common Mistakes:
  • Confusing tokens with data storage
  • Thinking tokens send notifications
  • Assuming tokens create projects
2. Which Firebase Admin SDK method is used to create a custom authentication token?
easy
A. admin.auth().createCustomToken(uid)
B. admin.database().createToken(uid)
C. firebase.auth().signInWithCustomToken(uid)
D. admin.storage().generateToken(uid)

Solution

  1. Step 1: Identify the correct Admin SDK service

    Custom tokens are created using the Authentication service, not database or storage.
  2. Step 2: Match the correct method

    The method to create a custom token is createCustomToken(uid) under admin.auth().
  3. Final Answer:

    admin.auth().createCustomToken(uid) -> Option A
  4. Quick Check:

    Custom token creation = admin.auth().createCustomToken [OK]
Hint: Use admin.auth().createCustomToken to generate tokens [OK]
Common Mistakes:
  • Using database or storage methods incorrectly
  • Confusing signInWithCustomToken with token creation
  • Calling methods on firebase instead of admin SDK
3. What will the following code output if 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));
medium
A. 'object'
B. An error message
C. 'undefined'
D. 'string'

Solution

  1. Step 1: Understand createCustomToken return type

    The method returns a Promise that resolves to a string token.
  2. Step 2: Check console.log output

    Logging typeof token will print 'string' because the token is a string.
  3. Final Answer:

    'string' -> Option D
  4. Quick Check:

    createCustomToken returns string token [OK]
Hint: createCustomToken returns a string token inside a Promise [OK]
Common Mistakes:
  • Expecting an object instead of string
  • Confusing token with undefined
  • Assuming an error without cause
4. Identify the error in this code snippet for generating a custom token:
const admin = require('firebase-admin');

admin.auth().createCustomToken('12345')
  .then(token => console.log(token))
  .catch(error => console.error(error));
medium
A. Missing admin.initializeApp() call
B. Incorrect method name, should be generateCustomToken
C. createCustomToken does not return a Promise
D. UID must be a string, not a number

Solution

  1. Step 1: Check initialization of Firebase Admin SDK

    The admin SDK must be initialized with admin.initializeApp() before use.
  2. Step 2: Verify other parts

    The UID '12345' is a valid string, createCustomToken returns a Promise, and method name is correct.
  3. Final Answer:

    Missing admin.initializeApp() call -> Option A
  4. Quick Check:

    Initialize admin SDK before calling methods [OK]
Hint: Always call admin.initializeApp() before using admin SDK [OK]
Common Mistakes:
  • Passing number instead of string UID
  • Assuming createCustomToken is synchronous
  • Using wrong method name
5. You want to add extra user info like role: 'admin' in a custom token. Which code snippet correctly adds this claim?
hard
A. admin.auth().setCustomUserClaims(uid, { role: 'admin' })
B. admin.auth().createCustomToken(uid).setClaims({ role: 'admin' })
C. admin.auth().createCustomToken(uid, { role: 'admin' })
D. admin.auth().createCustomToken(uid, { claims: { role: 'admin' } })

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    admin.auth().createCustomToken(uid, { role: 'admin' }) -> Option C
  4. Quick Check:

    Extra claims go as second argument in createCustomToken [OK]
Hint: Pass extra claims as second argument to createCustomToken [OK]
Common Mistakes:
  • Trying to chain setClaims on token creation
  • Using setCustomUserClaims instead of token claims
  • Nesting claims inside another object
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard