Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using generateToken instead of createCustomToken
Trying to use client SDK methods like signInWithCustomToken on the server
✗ Incorrect
The Firebase Admin SDK uses createCustomToken(uid) to generate a custom authentication token for the given user ID.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the claims object with a wrong variable name
Not passing any claims object when needed
✗ Incorrect
The second argument to createCustomToken is an object with additional claims, commonly named additionalClaims.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using verifyIdToken which is a server-side method
Using createCustomToken on the client side
✗ Incorrect
On the client side, use signInWithCustomToken(token) to sign in with a custom token.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using decodeToken which is not a Firebase Admin method
Accessing userId instead of uid
✗ Incorrect
Use verifyIdToken to verify the token and access the user's ID with decodedToken.uid.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using claims instead of additionalClaims as variable name
Not naming the catch parameter error
✗ Incorrect
Use createCustomToken(uid, additionalClaims) and catch errors with a parameter named error.