Search code examples
node.jsfirebasefirebase-authenticationfirebase-admin

Get User Display Name from Firebase VerifyToken


I have a custom back-end to which the client send the login firebase token which the server verifies and gets the decoded token, for the most part it contains enough information but I need the display name of the user with it to sync it with other clients, I can however make this by calling admin.auth().getuser(uid) I want to avoid making extra call and have other round trip just to get the display name.
Maybe I am overthinking but Isn't there a way to get that in a single call with verifyToken?


Solution

  • DecodedIdToken has many optional properties that are not explicitly listed. You can access them by treating the DecodedIdToken as a map. Following works as expected for me:

    const admin = require('firebase-admin');
    
    admin.initializeApp({
      projectId: '...',
    })
    
    const token = '....';
    admin.auth().verifyIdToken(token)
      .then((decodedIdToken) => {
        console.log(decodedIdToken.name); // Get user's display name
        // decodedIdToken['name'] if you're on TypeScript
      });
    

    The name property is only present when the user has signed in using a provider that exposes that information (such as google auth).