0
0
Firebasecloud~10 mins

Custom authentication tokens in Firebase - Interactive Code Practice

Choose your learning style9 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