How to incorporate roles in the useUser hook from nextjs-auth0?

Please include the following information in your post:

  • Which SDK this is regarding: nextjs-auth0
  • SDK Version: 1.9.1
  • Platform Version: N/A
  • Code Snippets/Error Messages/Supporting Details/Screenshots:
    Untitled-1

Hi,

I am struggling a bit with the process of using roles and permissions with auth0 . The desired outcome would be to have my roles and/or permissions added to my user on the useUser() hook from the nextjs-auth0 library.

Right now,

  • I have an API with RBAC enabled;
  • Created roles and permissions;
  • Assigned a role to my user;
  • Connected the APIs to each other;
  • And created this Login flow
exports.onExecutePostLogin = async (event, api) => {
  const namespace = 'https://__________.auth0.com';
  if (event.authorization) {
    api.idToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
    api.accessToken.setCustomClaim(`${namespace}/roles`, event.authorization.roles);
  }
};

But it does not show up when using getting my user. I created a “dummy” Login flow which adds the foobar: foobar2 bit that shows on my screenshot (...setCustomClaim("foobar", "foobar2")). I am not sure what I am missing here. The goal is to have the roles and permission to conditional rendering UI elements and afterword inside pages/api for proctecting certain actions.

Am I missing an important step?

Thanks.

Hey there @faabiopk welcome to the community!

Most of what you’ve described seems correct so that’s good :slight_smile: I don’t think the namespace you’ve used (auth0.com) will work per the guidelines here. Are you able to test using a different namespace or a non-namespaced value instead?

I also recommend checking out this post with regards to permissions:

Hope this helps!

1 Like

Thanks! That was it.

I was using my AUTH0_ISSUER_BASE_URL as the namespace. I thought it was something arbitrary, but changing to something else did the trick.

Thanks a lot for the quick response.

1 Like

Awesome, good to know!

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