# Best Practices for Retrieving User Permissions in a SPA (Vue.js) with Node.js API using Auth0

**URL:** <https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [November 30, 2023, 7:38am UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017 "2023-11-30T07:38:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![viniciusteixeiradias](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/viniciusteixeiradias/32/52741_2.png) [@viniciusteixeiradias](https://community.auth0.com/u/viniciusteixeiradias)\
**Post date:** [November 30, 2023, 7:38am UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/1 "2023-11-30T07:38:55Z")

</div>

I am developing a Single Page Application (SPA) using Vue.js with a Node.js API, where I am utilizing Auth0 for authentication and access control. However, I have encountered challenges in obtaining user permissions on the SPA side and am seeking guidance on best practices for handling this situation.

**Scenario:**

I have two endpoints in my API:

- `GET - /api/v1/projects /* read:project-list */`
- `GET - /api/v1/users /* read:user-list */`

And two corresponding screens in the SPA:

- `Project List /* read:project-list */`
- `User List /* read:user-list */`

My goal is to ensure that different users have access to specific functionalities based on their permissions. For example, the user Jhon, who is an administrator, has permissions to access both screens and endpoints, while James, a project manager, has access only to the “Project List” screen and endpoint.

**Challenges on the SPA:**

While I have successfully implemented permission validation in my Node.js API using features provided by Auth0, I am facing difficulties in finding the best practice for accessing this information on the SPA side.

**Considered Options:**

1. **Custom Endpoint:**

- Create a custom endpoint in the API that, based on the access token, returns the user’s permissions/roles.

1. **Use Auth0 API:**

- Create an API endpoint to consume https://{yourDomain}/api/v2/users/USER\_ID/permissions and retrieve the permissions/roles.

1. **Custom Claims in the Token ID:**

- Add permissions information directly to the Token ID and access the token through the SDK in the SPA `{ idTokenClaims } = useAuth0()`.

**Challenges with Custom Claims in the Token ID:**

I attempted to implement an action that sets the value within the Token ID but encountered difficulties in capturing user permissions, such as `read:users`. My current code:

```javascript
exports.onExecutePostLogin = async (event, api) => {
	const namespace = 'http://localhost:5173';
	const assignedRoles = (event.authorization || {}).roles;
	api.idToken.setCustomClaim(`${namespace}/roles`, assignedRoles);
};

```

**Question:**  
Given these options and challenges, I am seeking guidance on which approach is more recommended or if there is a standard practice for handling the retrieval of user permissions in a SPA with an API using Auth0. Any suggestions or similar experiences that can guide my decision would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 4, 2023, 10:43pm UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/3 "2023-12-04T22:43:11Z")

</div>

Hey there @viniciusteixeiradias !

Thanks for the detailed description, very helpful 🙂

> [@viniciusteixeiradias](#):
>
> Given these options and challenges, I am seeking guidance on which approach is more recommended or if there is a standard practice for handling the retrieval of user permissions in a SPA with an API using Auth0. Any suggestions or similar experiences that can guide my decision would be greatly appreciated.

I think your best bet is to add the permissions to ID tokens by way of the Management API/Node Management Client by way of an action as outlined here:

[https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506#h-12-permissions-4](https://community.auth0.com/t/how-to-add-roles-and-permissions-to-the-id-token-using-actions/84506#h-12-permissions-4)

---

<div class="post-metadata">

**Author:** ![viniciusteixeiradias](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/viniciusteixeiradias/32/52741_2.png) [@viniciusteixeiradias](https://community.auth0.com/u/viniciusteixeiradias)\
**Post date:** [December 5, 2023, 10:57am UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/4 "2023-12-05T10:57:51Z")

</div>

Hey @ty.frith !

Thanks for the suggestion and the reference to using the Management API/Node Management Client to add permissions to ID tokens. I appreciate it.

However, I’d like to delve deeper into the advantages of this approach compared to the solution I’m currently using, which involves creating a custom endpoint in the API that, based on the access token, returns user permissions/roles.

My current solution has been working well for me, but I’m always looking for optimizations and best practices. Could you share some specific advantages of the suggested approach?

Thanks again for the guidance!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 5, 2023, 11:21pm UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/5 "2023-12-05T23:21:42Z")

</div>

No problem, happy to help where I can!

> [@viniciusteixeiradias](#):
>
> My current solution has been working well for me, but I’m always looking for optimizations and best practices. Could you share some specific advantages of the suggested approach?

If your current solution works well and has auth in place then that’s totally OK as well - Using the custom claim/node management client approach is just the most straightforward without having to implement too much in the way customization 🙂

---

<div class="post-metadata">

**Author:** ![viniciusteixeiradias](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/viniciusteixeiradias/32/52741_2.png) [@viniciusteixeiradias](https://community.auth0.com/u/viniciusteixeiradias)\
**Post date:** [December 8, 2023, 12:03am UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/6 "2023-12-08T00:03:53Z")

</div>

Hey @ty.frith, thanks again for your help and your time.

I’m going to share my solution here to help anyone else going through the same situation because for me it was difficult to figure out how to do it. So I’m going to provide a solution using client management and also an endpoint to get this information:

- Client Management:

_Notes: You must use any user who has permission_;

```auto
/**
* Uses ManagementClient to get all user permissions
*
* @param {Event} event - Details about the user and the context in which they are logging in.
*/
const fetchPermissionNames = async (event) => {
  const ManagementClient = require('auth0').ManagementClient;
  const management = new ManagementClient({
    domain: event.secrets.domain,
    clientId: event.secrets.clientId,
    clientSecret: event.secrets.clientSecret
  });

  const params = { id: event.user.user_id, page: 0, per_page: 50, include_totals: false };
  const userPermissions = await management.getUserPermissions(params);

  return userPermissions.map(({ permission_name }) => permission_name);
}

/**
* Handler that will be called during the execution of a PostLogin flow.
*
* @param {Event} event - Details about the user and the context in which they are logging in.
* @param {PostLoginAPI} api - Interface whose methods can be used to change the behavior of the login.
*/
exports.onExecutePostLogin = async (event, api) => {
  try {
    const namespace = 'http://localhost:5173';

    const assignedRoles = (event.authorization || {}).roles;
    const permissionNames = await fetchPermissionNames(event);
  
    api.idToken.setCustomClaim(`${namespace}/roles`, assignedRoles);
    api.idToken.setCustomClaim(`${namespace}/permissions`, permissionNames);
  } catch (e) {
    console.error('Error on execute post login: ', e)
  }
};

```

- NodeJS API:

```auto
app.get('/permissions', jwtCheck, (req, res) => {
  const permissions = req.auth?.payload["permissions"] || []
  res.json(permissions);
})

```

.

This is basically how you can get these permissions.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 14, 2023, 1:26am UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/7 "2023-12-14T01:26:54Z")

</div>

That’s great @viniciusteixeiradias thanks a bunch for sharing with the community 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [December 28, 2023, 1:26am UTC](https://community.auth0.com/t/best-practices-for-retrieving-user-permissions-in-a-spa-vue-js-with-node-js-api-using-auth0/122017/8 "2023-12-28T01:26:56Z")

</div>

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