Search code examples
next.jsauth0next-auth

NextJs Auth0 - get user_metadata from user session middleware


i'm trying to get a user_metadata from the useUser hook. Here is what i've tried.

Auth Action

exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://my-tenant-auth0.com';
  api.idToken.setCustomClaim(`${namespace}/user_metadata`, event.user.user_metadata);
  api.accessToken.setCustomClaim(`${namespace}/user_metadata`, event.user.user_metadata);
};

NextJs Middleware.

const afterCallback = (req, res, session, state) => {
   session.user.idToken = session.idToken;
   session.user.testVar = JSON.stringify(session);

   return session;
};

No user_metadata in session variable. Also when i console.log(session) inside afterCallback for some reason the console.log() isn't printing anything.


Solution

  • This is actually working, i was just missing one step which is 'activating' the action. On the dashboard under actions/flows/login/. Drag and drop the action. dashboard screenshot