From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Applying multiple roles

Applying multiple roles

In some applications, it is necessary to assign multiple roles to users to ensure that they have the appropriate permissions based on their responsibilities. In order to provide multiple roles on the route, we just have to update the roles decorator. For example, let's give the user role to the post route handler. Now, when we try to make a POST request as a user, I'll give the API key user B102 and the user is added. Let's make the GET request as well to check the new user giving the API key as user D104 and the user data is displayed. So this is how we can provide multiple roles to a route handler. Now few things to remember when giving multiple roles is in the roles.decorator file, we have to provide the role enum of type array. Because then only we can assign multiple roles on a route handler. Also even if we give the role enum as array and do not provide the spread operator, then you can see the error expected one argument but got two. So again, we cannot provide multiple roles…

Contents