I am not getting email from my accessToken

Hello,

I am using NextJS for frontend and NestJS for backend. I created a custom claim to get email in my acessToken.

exports.onExecutePostLogin = async (event, api) => {
  // This adds the authenticated user's email address to the access token.
  if (event.authorization) {
    const namespace = 'http://localhost:3000';
    api.accessToken.setCustomClaim(`${namespace}/claims/email`, event.user.email);
  }
};

This is what I am getting from jwt.io:

{
  "iss": "https://dev-escroid-1.us.auth0.com/",
  "sub": "DjoYwj2Xh3tbnoysfn3nQUrY2LAVwDP5@clients",
  "aud": "http://localhost:3000",
  "iat": 1679752328,
  "exp": 1679838728,
  "azp": "DjoYwj2Xh3tbnoysfn3nQUrY2LAVwDP5",
  "gty": "client-credentials",
  "permissions": []
}

On next js, I am using this library from auth0:

import { handleAuth, handleLogin, handleCallback } from "@auth0/nextjs-auth0";



  export default handleAuth({
  login: handleLogin({
    authorizationParams: {
      audience: process.env.AUDIENCE, // or AUTH0_AUDIENCE
      // Add the `offline_access` scope to also get a Refresh Token
     scope: 'openid profile email' // or AUTH0_SCOPE
    }
  })
});  

I am using this code to get my accessToken from Auth0 after login:

 var options = {
      method: "POST",
      url: 'https://dev-escroid-1.us.auth0.com/oauth/token',
      headers: { 'content-type': 'application/json' },
      body: '{"client_id":"ID","client_secret":"Secret","audience":"http://localhost:3000","grant_type":"client_credentials"}' };
    

    request(options, async function (error, response, body) {
      console.log("res", body)

Please how can I get email and ID in my nextJS accessToken

Hi @olusanya,

Welcome to the Auth0 Community!

A client credentials grant is a user-less grant type that covers Machine to Machine auth. The reason you aren’t getting an email is because there is no user involved.

Let me know if you have any questions.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.