How do I get User Metadata in the Login

Please include the following information in your post:

  • SDK Verion: express-openid-connect: “^2.5.1”,
    *Platform Version: NodeJs 16.6.2

I am using nodejs and express openid connect and I am trying to get ALL of the user the metadata from auth0.

here’s the route:

const authenticationController = require("../controllers/auth.controller");
const { auth, requiresAuth } = require('express-openid-connect');

module.exports = function(app, cors, corsOptions) {
    app.get('/authentication', 
        cors(corsOptions),  
        requiresAuth(),
        function (req, res, next) {
            authenticationController.GetAuthentication( req, res, next );
    });
};

here’s the controller:

exports.GetAuthentication = async ( req, res, next ) => {
    try {
        var user = req.oidc.user;
        return res.status(200).json(user);
     } catch(err) {
       return next(err)
     }
  };

Here is the output:

{"nickname":"mynick"
,"name":"First Last",
"picture":"https://s.gravatar.com/avatar/xxx?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fso.png",
"updated_at":"2021-10-17T15:39:14.845Z",
"email":"user@domain.tld","email_verified":true,"sub":"auth0|123456xxx"}

As you can see none of the metadata for my user is showing up in the user object. How do I get the metadata?

Hey @solid,

The reason you didn’t receive the user’s metadata is because metadata is not a standard claim so it will not be included in ID Tokens by default.

To include it, you must write a Rule to copy user_metadata properties to the ID token. This is explained in detail here: Add custom claims to a token. I hope this helps.

More information:

1 Like

Thanks for helping on this one Ale!