Adding email to access token

Hi! I have a custom database connection which passes the credentials from React app to the backend. If the request to the login endpoint was successful then I’m getting the token in React. The problem is that I can’t include user email in access token. It’s required for further usage it with secured backend endpoints directly. What is the proper way of adding it to the token? I’ve tried using, Hooks, Rules, Actiobns (flows and library), but the token claim structure stays the same
“sub”: “auth0|xxxxxxxxxx”,
“aud”: [
https://xxxxxxxxxxx”,
https://xxxxxxxxx
],
“iat”: 1684859970,
“exp”: 1684946370,
“azp”: “xxxxxxxxxx”,
“scope”: “xxxxxxxx”
}

Hi there @vadzim.vashchanka welcome to the community!

Assuming you have the Action deployed and bound to the login flow properly, the following code should work:

exports.onExecutePostLogin = async (event, api) => {

  if (event.authorization) {
    api.accessToken.setCustomClaim('custom_email_claim', event.user.email)
    console.log(`Logging user's email: ${event.user.email}`)
     }
  }

I added the logging as it can be helpful for debugging - You should be able to see the logging with the “Actions Details” tab of the successful login in your dashboard Monitoring → Logs as well as when using the Real-time Webtasks Log extension.

Keep us posted!

2 Likes

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