A developer implements JWT-based authentication in a NestJS application. The process includes creating a custom parameter decorator to extract the user, configuring the JWT module with environment variables, and setting an HTTP-only secure cookie on the response for authenticating subsequent requests.
Length 19:01
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #nest.js #jwt #authentication #backend #programming #typescript #cookies #web development #coding tutorial #software engineering
0:01 All right, so in this lecture, we're ready to go ahead 0:02 and get started in setting 0:04 up our JSON web token functionality so that in 0:08 our auth controller, after we've successfully validated 0:11 that the user's 0:12 credentials are indeed valid, we then want to 0:15 produce a JSON web token back to that user that they can use 0:19 to then authenticate with our application later on in subsequent requests 0:24 and it's how we're gonna protect our other routes. 0:26 Now, the first thing we need to do in order to 0:28 implement this is to 0:30 actually be able to pull off the user that we return 0:34 from this validate method in our local strategy. 0:37 So if you remember, Passport is going to set on the request 0:41 object as the user property whatever we return from 0:45 this validate method. We want to then extract that 0:49 user in this login route after we've gone 0:53 through the local auth guard and extract the user from the request so 0:56 we 0:57 know the current authenticated user in the request. 1:01 To do this, we can take advantage of NestJS param 1:04 decorators 1:05 to extract the user property on this given request. 1:09 All right, so let's go ahead and create a new file to host this 1:11 parameter 1:12 decorator. 1:13 In the auth directory, I'll create a new 1:16 currentuser.decorator.ts 1:21 and now in here, I want to export const current 1:25 user equal to createParamDecorator from 1:29 nestjs common. So we can see here this is going to accept 1:33 a factory method that will allow us to 1:36 return a custom HTTP route param 1:40 decorator. So let's go ahead and provide this factory in the form of a 1:44 function that gets access to two parameters. 1:47 We can see the first is the data that we can pass 1:50 into this decorator if we have some data 1:55 from the incoming call, for example, that we need to provide.
The rest of the transcript is for members.
Sign in