The developer implements cross-service JWT authentication in a NestJS microservice architecture. By configuring TCP transport, creating a shared Auth Guard, and updating controllers, they enable secure inter-service communication and protect reservation routes, ultimately testing the authentication flow with Postman.
Length 26:14
Captions English (Generated), Spanish (Auto-translated), French (Auto-translated), Korean (Auto-translated), Portuguese (Auto-translated), Vietnamese (Auto-translated) #nestjs #microservices #authentication #jwt #tcp #typescript #backend #programming #rpc
0:00 Okay, so we have the ability to apply 0:01 authentication to our routes 0:03 directly inside of the auth app right now. 0:07 However, I want to be able to add authentication to 0:10 our 0:10 reservations controller so that any of these routes 0:14 here will be protected by a JWT Auth Guard 0:18 and the user must be authenticated to access these routes. 0:22 So in order to do this, we need to have a way to connect our 0:25 microservices 0:25 together so that the reservations can talk to 0:29 auth and authenticate a user. NestJS offers this 0:33 out of the box with a number of different transport options to support 0:37 networking between our microservices. 0:39 We're gonna use a standard TCP-based transport layer 0:44 to be able to connect our microservices together. 0:47 So let's go ahead and start wiring up our auth service to be 0:50 able to 0:50 talk as a microservice. Firstly, we're gonna go back into 0:54 the terminal, and we're gonna go ahead and pmm install 0:58 nestjs/microservices. So we can go 1:01 ahead and start our server back up by running docker-compose up 1:05 here. And now inside of our auth service, we're 1:09 gonna convert this into a hybrid application, one that listens 1:13 for both incoming HTTP connections in our controllers as 1:17 well as over our microservice layer through TCP. 1:21 So in order to accomplish this in our main.ts, we're gonna go ahead and call 1:24 App.connectMicroservice, which will allow us to 1:28 specify an options object here where we can specify the 1:32 transport that we want. So like I said before, there 1:35 are a number of different 1:36 transport options available, including Redis, RabbitMQ, and 1:40 Kafka, which I have covered in past videos 1:42 and I may add to this course as 1:44 well. However, for now, we're gonna make use of just a standard 1:47 TCP 1:47 protocol. So we can set this with the transport
The rest of the transcript is for members.
Sign in