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 role for specific handlers - Nest.js Tutorial
From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework
Applying role for specific handlers
In many applications, especially those with diverse user bases, it's crucial to control access over certain features, or route handlers based on user roles. And that's what we will be seeing in this example. That is how we can set certain route handlers for specific users only. So let's begin. First inside the user service file, I will create one more method that is addUser method. To add a new user, I will give the user of type any in the argument and return this.usersArray.push and user. Now inside the controller, let's define the post route handler. I'll give the post handler and the route path as create. We'll give a method called create, which takes the body decorator, the user data property of type any to store the request body. Then to add the user, I'll give this dot user service dot add user, user data, and we'll return a message user added successfully. Now the next step is somewhat related to the database. Right now we don't have any database connected. So we need to create…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Introduction to guards4m 14s
-
(Locked)
Understanding guard5m 33s
-
(Locked)
Understanding ExecutionContext6m 50s
-
(Locked)
Accessing arguments with getArgs()3m 42s
-
(Locked)
Limiting controller access with guard4m 6s
-
(Locked)
Understanding switchToHttp() method4m 59s
-
(Locked)
Assignment: API key authorization6m 10s
-
(Locked)
Applying multiple guards3m 14s
-
(Locked)
Defining custom metadata5m 6s
-
(Locked)
Setting custom metadata: A better way3m 25s
-
(Locked)
Applying role for specific handlers10m 3s
-
(Locked)
Applying multiple roles1m 35s
-
(Locked)
Applying global guards4m 46s
-
(Locked)
-
-
-
-
-
-
-