Getting the Current User with a Custom Decorator
Learn how to create a custom decorator in NestJS.
We'll cover the following...
After setting up our guard and assigning the user to the request object in our application, we need to access this user data in our controllers. This section will explore efficiently retrieving user data using a custom decorator.
Implementation
Suppose we need to directly access user data from the request object in the findBooks method of our BooksController. Let’s see how to do that in the following code:
Collection
| Key | Value | Description | |
|---|---|---|---|
RESPONSE
Enter the URL and click Send to get a response
Press “Run” to start the development server.
Note: Before using the
get list of booksendpoint, it’s essential to log in to the application. Start by registering with thesign-uprequest and then log in using thesign-inrequest to obtain your access token. Once you have the token, include it in the authorization headers for theget list of booksrequest, formatted asBearer <token>. This is a crucial step to ensure ...