WEBVTT

00:00.080 --> 00:00.290
Okay.

00:00.380 --> 00:08.180
So in order to start implementing our JWT authentication with Passport, we'll go ahead and stop our

00:08.180 --> 00:12.530
app from running and we'll need to install some new dependencies here.

00:12.680 --> 00:18.920
I'll go ahead and install nestjs slash passport as well as passport itself.

00:18.920 --> 00:24.000
And then finally passport local here, which will be our first authentication strategy.

00:24.020 --> 00:30.500
We'll also go ahead install the types for passport local here as a development dependency.

00:30.500 --> 00:36.410
So we're also going to want to install the dependencies to be able to implement our JWT authentication.

00:36.410 --> 00:44.930
So let's go ahead and NPM install Nestjs, slash JWT as well as Passport, JWT and Nestjs.

00:44.960 --> 00:50.780
JWT is a package that helps with the JWT manipulation and then we'll go ahead and install the development

00:50.780 --> 00:53.990
dependencies for types slash passport, JWT.

00:54.110 --> 01:00.420
And once we have this installed, we'll go ahead and start our app back up by running Docker compose

01:00.420 --> 01:00.900
up.

01:00.900 --> 01:04.890
So now we're actually ready to start writing the code for the auth service here.

01:04.890 --> 01:08.700
So let's go ahead and open up the auth module.

01:08.700 --> 01:21.630
We're going to have to import the JWT module now and we import the module up top from Nestjs slash JWT.

01:21.750 --> 01:29.010
So we need to configure the module with some environment variables and we can accomplish this by calling

01:29.010 --> 01:31.110
register async on this.

01:31.110 --> 01:36.390
And inside of here we can use a use factory and get access to the config service.

01:36.390 --> 01:41.870
And so we're going to return an object here that has some environment variables.

01:41.880 --> 01:49.230
The important thing being the JWT secret value, which is the special token that the JWT module uses

01:49.230 --> 01:54.900
to actually verify incoming JWT against to make sure that they're valid.

01:54.900 --> 01:57.810
So when we sign a new key, we use the secret.

01:57.810 --> 02:00.000
And when we verify, we use the same secret.

02:00.000 --> 02:04.500
So it's important that we keep this hidden away in an environment variable.

02:04.500 --> 02:12.720
So we'll call a config service service here, dot get type string and we'll pull the secret, which

02:12.720 --> 02:14.940
we don't have yet, but we will add soon.

02:14.940 --> 02:21.510
And then we're also going to add sign options here where we can specify an expiration date on the token.

02:21.510 --> 02:27.660
So I'll go ahead and create a string template literal here where we call the config service dot, get

02:27.690 --> 02:30.870
JWT expiration.

02:31.050 --> 02:36.390
And I'm also going to put an S at the end here so that we turn this value into seconds.

02:36.390 --> 02:41.970
So now we have the JWT module all configured, but of course we don't actually have these environment

02:41.970 --> 02:43.830
variables set up yet.

02:43.830 --> 02:52.830
So let's go into our dot env at our root here and I've just used a random string generator to get a

02:52.860 --> 02:57.060
JWT secret value here that I'll go ahead and paste in here.

02:57.060 --> 03:05.190
And then for the JWT expiration, I'll give it a value of 3600 for 3600 seconds.

03:05.190 --> 03:08.760
So now we have these environment variables for the auth service.

03:08.790 --> 03:10.680
This is everything we need, which is great.

03:10.680 --> 03:16.050
However, it's not really great here how we're actually mixing all of our different environment variables

03:16.050 --> 03:21.900
for each one of our microservices when we don't actually need these environment variables for any of

03:21.900 --> 03:23.490
the other microservices.

03:23.490 --> 03:32.190
So let's go ahead and start addressing this by creating a unique dot m file for just our auth service.

03:32.190 --> 03:38.940
And I'll go ahead and copy and paste all of these m variables into this new dot m for just the auth

03:38.940 --> 03:46.440
and then I'll move the root dot env the original one, I'll move that into the reservation service and

03:46.440 --> 03:51.120
get rid of the JWT information here now as well.

03:51.120 --> 03:57.450
And so now what we'll have to do is open up our docker compose and actually tell Docker, compose where

03:57.450 --> 04:00.060
to look for these M files.

04:00.510 --> 04:08.460
So for our reservation service, I'll have a new M file key here where I'll specify where this dot m

04:08.460 --> 04:09.180
is located.

04:09.180 --> 04:16.410
In this case it's apps slash reservations slash dot env and then we'll do the same thing for auth here.

04:16.410 --> 04:23.480
We'll have an m file and we'll point it to dot slash app slash auth slash dot env.

04:23.580 --> 04:29.070
So now each one of these services is going to have their own individual dot env, which makes more sense.

04:29.070 --> 04:36.390
And next, if we go back into the auth dot module, you can of course see that we are introducing new

04:36.390 --> 04:37.800
environment variables here.

04:37.800 --> 04:45.060
So what we'd like to do is in our config module we of course want to validate these environment variables

04:45.060 --> 04:47.070
to make sure that they actually exist.

04:47.100 --> 04:53.040
However, it's not great that we'll have to validate all environment variables for all of our services

04:53.070 --> 04:54.330
in this one place.

04:54.330 --> 04:59.790
So to get around this, I'm actually going to refactor our current approach towards configuration.

04:59.830 --> 05:05.710
I'll go ahead and get rid of the config directory altogether in lib slash common.

05:05.710 --> 05:10.810
So we only have the database and the logger, which of course is going to make our code a bit angry

05:10.810 --> 05:16.900
because now the database module can't find the config module and that's okay actually.

05:16.900 --> 05:23.080
So to fix this here, let's get rid of the config module import here and we'll get rid of it from the

05:23.080 --> 05:24.280
database module itself.

05:24.280 --> 05:31.000
The database module will just rely on the service that's calling the database module to have already

05:31.000 --> 05:33.310
set up the config module here.

05:33.310 --> 05:34.720
So let's go ahead and do that.

05:34.720 --> 05:35.290
Exactly.

05:35.290 --> 05:42.550
Let's go into reservations module and let's set up the config module directly in here.

05:42.550 --> 05:48.730
So this way in each individual microservice, we can set up the config module specifically just for

05:48.730 --> 05:53.470
this service, which I think is a better approach than how we were previously doing it.

05:53.470 --> 05:59.680
So let's go ahead and import the config module from nestjs config and we'll call for route as we've

05:59.770 --> 06:00.370
done.

06:00.370 --> 06:07.810
And this time I'm going to set it to global so that the config module will be available to anyone that

06:07.810 --> 06:09.970
needs it in this service.

06:09.970 --> 06:14.200
And then we can add the validation schema back here.

06:14.200 --> 06:23.170
So in this case, remember it was that joy dot object key here and we'll of course import Joy again

06:23.170 --> 06:26.950
as import star as joy from Joy.

06:26.980 --> 06:37.870
So we'll open up this object here and we'll add the MongoDB Uri back as a joy dot string dot required.

06:38.170 --> 06:44.920
So now we have this config set up for reservations and you can see reservations starts up fine again

06:44.920 --> 06:48.250
and then we'll do the same thing in the auth dot module.

06:48.250 --> 06:53.740
I'll actually copy reservations module setup we have here, so I'll copy this config module.

06:53.950 --> 06:59.080
We'll go back to the auth module and let's add the config module here.

06:59.380 --> 07:02.020
We'll have to add the imports as well.

07:02.380 --> 07:07.360
I'll import Star as joy from Joy here.

07:07.390 --> 07:12.250
I think this an uppercase J and now we have that MongoDB URI, which is great.

07:12.250 --> 07:18.340
But now we also want the JWT we also know will be a required string.

07:18.340 --> 07:24.040
So we'll add that required here as well as the JWT expiration.

07:24.040 --> 07:31.000
So these environment variables will only be checked in the auth service, which makes a lot more sense.

07:31.330 --> 07:40.390
And now let's go back to our terminal here and actually restart our containers so that we load in the

07:40.390 --> 07:46.330
dot m files that we updated in the Docker compose and you can see the reservations module starts up

07:46.330 --> 07:47.320
successfully.

07:47.320 --> 07:50.080
The auth module is having some issues here.

07:50.080 --> 07:51.910
So let's go ahead and take another look.

07:51.940 --> 07:59.710
The issue is here is we're not actually injecting the config service, so make sure we add the inject

07:59.710 --> 08:06.160
property here and we'll inject the config service to the JWT module so it actually can inject these

08:06.160 --> 08:06.790
values.

08:06.820 --> 08:09.190
Now we can see auth starts up successfully as well.

08:09.190 --> 08:15.880
So one more thing I want to do now that we have actually made the config module scoped to each individual

08:15.880 --> 08:19.870
service as well as the M is in our main.ts right now.

08:19.870 --> 08:25.240
We're hardcoding the port that we're listening on, which will not be great when we actually want to

08:25.240 --> 08:26.440
deploy our apps.

08:26.440 --> 08:28.090
So let's go ahead and fix this.

08:28.090 --> 08:36.160
And in order to fix this, let's pull the config service out here by calling app dot, get config service

08:36.160 --> 08:42.880
and we're pulling the nestjs config service here directly by calling app dot get, which allows us to

08:42.880 --> 08:44.290
retrieve any injectable.

08:44.290 --> 08:50.470
And now instead of listening on this hardcoded port, I want to actually call config service dot get

08:50.470 --> 08:56.830
and then get the port here so that we're actually going to get the port value and I'll do the same thing

08:56.830 --> 08:58.150
in our reservation service.

08:58.150 --> 09:06.310
So let's copy this, go to our reservations Main.ts and we'll paste the same thing in here as of course

09:06.310 --> 09:08.860
we'll need to import the config service as well.

09:08.950 --> 09:12.610
Now we'll actually need to add the port to our M's.

09:12.610 --> 09:19.900
So I'll add a port here for the reservation service to listen on to port 3000 and then I'll do the same

09:19.900 --> 09:22.000
thing for the auth service.

09:22.000 --> 09:30.190
But this time we want to listen on Port 3001 and then finally we'll add a new validation check here.

09:30.190 --> 09:32.530
Make sure we have a port this time.

09:32.530 --> 09:37.960
We know it's going to be a number that's required and same thing for the auth.

09:37.960 --> 09:47.020
We will check that the port is also included and that it is a number here.

09:47.020 --> 09:52.810
Now, you might also need to restart your containers as well to make sure we get that updated dot m

09:52.810 --> 09:56.440
file and we can see everything starts up successfully here.
