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.

Assignment: API key authorization

Assignment: API key authorization

In this assignment, we are going to create a guard that allows user authorization on the basis of the API key assigned to the object. The request should proceed if the user inputs correct API key assigned to the object. Otherwise, for incorrect API key value, the request should not be processed, and an unauthorized error should be displayed. So let's get started. I will create a service file by giving the command nest generate service slash services, naming the service file as user. Inside the user service, I will create a user's array object. I'll set the API key as property and will assign a secret API key. Then a name property with a name value. and an email property with an email value. I'll create two more objects user B and user C. Then I'll create a method naming it as get user and passing the API key as a parameter which is of type string. We will retrieve the user data based on the API key. So inside this method, let me give return this.users.find. In the callback, I'll pass…

Contents