WEBVTT

00:03.760 --> 00:05.350
Hey did everyone had this year?

00:05.350 --> 00:07.260
And let's move forward in this video.

00:07.270 --> 00:09.670
In this video, we'll be touching a little bit of the database.

00:09.670 --> 00:14.410
First, we need to fix that unauthorized and have to understand what's actually happening in the callback.

00:14.680 --> 00:21.220
So one of the thing which passport does, among many other things is if I look at the passport JRS notice

00:21.220 --> 00:22.750
here we are receiving this callback.

00:22.750 --> 00:24.670
So we have this callback all going in.

00:24.670 --> 00:26.500
We have access, token refresh and everything.

00:26.500 --> 00:27.910
The profile is coming in.

00:27.910 --> 00:34.600
Now, not only that, all that information that is coming in into your object, that actually goes a

00:34.600 --> 00:35.980
little bit further down the road.

00:35.980 --> 00:37.840
So let me walk you through what is coming in.

00:37.840 --> 00:43.870
So I'll go into this odd dodges and instead of saying that this one is going all in, let me go ahead

00:43.870 --> 00:47.710
and remove this and I'm going to go ahead and say request dot user.

00:48.130 --> 00:52.570
You might be saying, hey, we don't have any middleware, we don't have ID, JSON token, we haven't

00:52.570 --> 00:53.470
injected anything.

00:53.470 --> 00:56.290
So how this request to use user is going to come in.

00:56.290 --> 01:00.610
It will come in thanks to the courtesy of our password, guys, but not right now.

01:00.610 --> 01:05.230
Let me go ahead and save this one and show you again that some of the stuff that is not going to work

01:05.230 --> 01:08.320
again one more time, not how we expect them to work.

01:08.320 --> 01:10.030
So let me go ahead and refresh that.

01:10.030 --> 01:13.840
I'm going to go ahead and say, hey, this time, let's go ahead and log in Google.

01:14.590 --> 01:18.940
Same login we go up here, it takes a little bit time and it says unauthorized.

01:18.940 --> 01:23.800
Now the reason why it is saying unauthorized, because we haven't actually take the full advantage of

01:23.800 --> 01:29.050
the password yet because honestly, this is not supposed to be worked that fast.

01:29.050 --> 01:30.670
So let's go ahead and work on with that.

01:30.700 --> 01:34.960
Now notice here, I'll come back onto this part that, hey, right now we were able to see nothing at

01:34.960 --> 01:35.260
all.

01:35.260 --> 01:39.550
We haven't written any middleware, we haven't injected any value, but we'll come on to this part.

01:39.850 --> 01:46.540
First thing is now I have this profile that means I can access the user's email by simply saying profile,

01:46.780 --> 01:48.340
underscore JSON, email.

01:48.340 --> 01:49.570
So that is one clear.

01:49.720 --> 01:52.450
Now before I move ahead and go ahead and say, hey, just go ahead.

01:52.450 --> 01:55.870
And next, I would like to add an entry in my database.

01:55.870 --> 02:02.530
So I'll bring in my user model and I'll say that search for this email into my database if it doesn't

02:02.530 --> 02:04.420
exist, make an entry in the database.

02:04.420 --> 02:07.450
If it exists, just allow the user to further move on.

02:07.450 --> 02:09.850
Let's go ahead and work on that how this is going to work.

02:10.330 --> 02:12.730
So I have this Google strategy and passport.

02:12.730 --> 02:14.530
Let's go ahead at the top of it.

02:14.530 --> 02:15.790
Let's bring the user as well.

02:15.790 --> 02:18.670
So let's go ahead and say, hey, I want to bring in a user.

02:18.790 --> 02:26.650
That user will be coming up from require and let's go one directory back inside the model and there

02:26.650 --> 02:27.700
we go user.

02:27.760 --> 02:31.210
So this user is up here and now I can access this one.

02:31.210 --> 02:37.060
So again, by the way, just to remind you again, this is again up here so I can go ahead and say underscore

02:37.060 --> 02:38.650
Jason and dot email.

02:38.650 --> 02:39.820
I can access that again.

02:39.820 --> 02:43.090
Better option would be in this case to access this through the emails.

02:43.090 --> 02:46.120
But yeah, this is pretty much consistent, no such big issue.

02:46.390 --> 02:51.190
So this one is going to be profile, dot, underscore Jason and dot email.

02:51.190 --> 02:52.930
So this is how this is going to come in.

02:53.260 --> 02:53.590
Okay.

02:53.590 --> 02:58.840
Further down the road before I go ahead and do all these stuff, I will ask my database that, hey,

02:58.840 --> 03:00.670
go ahead and find one user for me.

03:00.670 --> 03:01.870
So let's go up here.

03:02.050 --> 03:05.980
I don't have a big promise wrap around, so I have no much offer option.

03:05.980 --> 03:08.080
I have to use them and catch quite a lot.

03:08.260 --> 03:14.110
So I'm going to say, Hey model, just go ahead and please find one for me and you'll be searching based

03:14.110 --> 03:15.190
on the email.

03:15.370 --> 03:22.660
In the email, I'm going to go ahead and provide profile dot underscore Jason Dot email.

03:22.660 --> 03:26.230
So this will give you the email now if the user exists.

03:26.230 --> 03:32.680
So we're going to go ahead and say dot then and we can actually go ahead and say that if the then is

03:32.680 --> 03:35.590
going to exist, then it is going to exist based on the user.

03:35.590 --> 03:37.330
So it will return us that user.

03:37.450 --> 03:38.350
There we go.

03:38.410 --> 03:41.530
Now let's go ahead and quickly check if the user exists or not.

03:41.560 --> 03:46.480
Now, if the user exists, that means user was present in the database.

03:46.480 --> 03:49.120
So I'm going to go ahead and simply do a console log.

03:49.120 --> 03:52.210
So log if it can suggest me.

03:52.210 --> 04:01.300
There we go, console log and we're going to go ahead and say user already exists in DB and let's also

04:01.300 --> 04:03.250
throw up this user whatever we have bring in.

04:03.250 --> 04:04.570
So this is the user.

04:04.750 --> 04:09.190
Now in that case, that means I'm all done and things are working fine.

04:09.190 --> 04:12.640
So I'm going to go ahead and notice this time.

04:12.640 --> 04:15.370
Very, very importantly, we haven't done this in the past.

04:15.370 --> 04:17.200
I'm going to use this next.

04:17.200 --> 04:19.690
Now, this next is little bit special.

04:19.690 --> 04:24.610
In the case of Passport, the first parameter it takes is null, and the second parameter that it takes

04:24.610 --> 04:25.300
is the user.

04:25.300 --> 04:27.580
Whatever the user you have, you are giving it to this.

04:27.580 --> 04:30.610
In this case, I'm giving it the user from my database itself.

04:30.850 --> 04:32.320
Previously we haven't done this.

04:32.320 --> 04:33.430
Next, that is fine.

04:33.430 --> 04:35.200
We'll work on with that part as well.

04:35.230 --> 04:36.850
Right now I just want to comment this.

04:36.850 --> 04:38.260
We won't be using that part.

04:38.500 --> 04:38.950
Okay.

04:39.490 --> 04:40.840
So this is the if case.

04:40.840 --> 04:45.670
If I have actually gone ahead and found the user, what about if I haven't found the user?

04:45.670 --> 04:51.070
And again, we need to bring this cut it out and bring it up in the if case.

04:51.310 --> 04:52.000
There we go.

04:52.120 --> 04:54.760
So if user exist, go ahead and pass on this.

04:54.760 --> 04:56.770
We have used this next a little bit differently.

04:56.770 --> 04:58.210
This time we are passing on null.

04:58.240 --> 04:59.950
This is how the passport is designed.

04:59.950 --> 05:01.840
It's not nothing to do with the express itself.

05:02.110 --> 05:04.990
And the second value that I'm passing on is the user itself.

05:05.200 --> 05:09.820
Now, in the case, if we haven't found the user, then in that case, what we really want to do, we

05:09.820 --> 05:11.340
want to create an entry in the database.

05:11.350 --> 05:17.320
So let's go ahead and say user, dot, create and in the create part, let's go ahead and pass on the

05:17.320 --> 05:17.680
object.

05:17.680 --> 05:19.480
So we have a name.

05:19.780 --> 05:25.480
The name is going to come up from a profile dot display.

05:25.480 --> 05:27.310
Let me go ahead and check that.

05:28.610 --> 05:30.470
So where is the name?

05:30.710 --> 05:31.160
There we go.

05:31.190 --> 05:32.120
Display name.

05:33.320 --> 05:34.460
So display name.

05:34.460 --> 05:36.650
Let's go ahead and make a copy of this one.

05:37.660 --> 05:42.310
And inside our model, we call this one as Google ID like that.

05:43.210 --> 05:44.410
So let's go up here.

05:44.410 --> 05:46.360
And this one is Google ID.

05:46.390 --> 05:51.010
So this is profile dot ID, all lowercase.

05:51.130 --> 05:52.990
And then we have finally the e mail.

05:52.990 --> 05:54.940
So let's go ahead and use that.

05:55.630 --> 06:00.460
And in fact, I can just copy this from here and paste it up here.

06:00.640 --> 06:01.420
So there we go.

06:01.420 --> 06:03.610
Now there is a new entry in my database.

06:03.610 --> 06:06.000
If this entry is successful, we can chain on a dot.

06:06.010 --> 06:10.570
Then I know some of you don't like this much of chaining of the things, but let's go ahead and work

06:10.570 --> 06:13.930
on with that and we're going to have a dot catch just like that.

06:13.930 --> 06:21.520
In that case, if we receive an error, we're going to go ahead and say, hey, console dot, log this

06:21.520 --> 06:26.620
error and in case everything goes fine, then that means this will give us a user back.

06:26.620 --> 06:29.940
So let's go ahead and take this user work like that.

06:29.950 --> 06:32.710
In that case, I'll print out a console, log on the screen.

06:32.710 --> 06:34.630
So I'm going to go ahead and say log

06:37.120 --> 06:38.290
user.

06:38.530 --> 06:42.520
Let's call this one as simply new user and also throw back that user.

06:42.520 --> 06:47.440
Now, most importantly, just like we have used the next here, also we have to use the next and the

06:47.440 --> 06:48.520
first parameter is null.

06:48.520 --> 06:49.660
This is actually an error.

06:49.660 --> 06:54.160
So we expect we won't be handling any error in this case and we're going to go ahead and pass on this

06:54.160 --> 06:54.580
user.

06:54.580 --> 06:58.900
Now, the most important thing, what is going to happen when we pass on this user?

06:58.900 --> 07:01.360
Previously we were facing this unauthorized.

07:01.360 --> 07:07.510
That was because behind the scene this password goes in and tries to work the middleware that we have

07:07.510 --> 07:12.820
approached in the last section, remember, we were having this ID, we were creating this JSON token,

07:12.820 --> 07:18.910
then we were kind of demystifying or extracting the values from the token, the ID itself, creating

07:18.910 --> 07:23.530
our database, and we were putting all these things into request user.

07:23.530 --> 07:28.630
This is so much of a standard practice that passport GC also does the exact same for you.

07:28.630 --> 07:34.720
So when you say next and you pass on this user next year, this passport will automatically come in.

07:34.720 --> 07:40.240
We'll create a property user so you can access that by request dot user and we'll add on all the properties

07:40.240 --> 07:40.660
here.

07:40.660 --> 07:46.090
And this is exactly the reason why I have told you here that request user So just like we were expecting

07:46.090 --> 07:50.410
that in the t shirt store that hey, we were doing everything manually, we don't have to do this this

07:50.410 --> 07:50.860
time.

07:50.860 --> 07:53.290
Passport guys will take care of the things for us.

07:53.560 --> 07:56.890
Okay, so this is quite a lot that we have worked on, so let's go ahead and try this time.

07:56.890 --> 07:59.200
So let's go into the database, hit refresh.

07:59.200 --> 08:02.860
We have passport and the user right now, there is nothing inside the user.

08:02.860 --> 08:05.890
Let's go back onto the screen and see if it is working or not.

08:06.130 --> 08:08.980
So again, localhost and let's go back up here.

08:09.460 --> 08:11.560
So this is our login screen.

08:11.560 --> 08:18.130
Let's go into the Google and again my email and let's go ahead work on with that and it says fail to

08:18.130 --> 08:20.110
serialize user into the session.

08:20.110 --> 08:22.570
Now again remember we have this cookie session.

08:22.570 --> 08:24.580
Yes, that is the reason why it is failing.

08:24.580 --> 08:28.810
But in theory, let's go ahead and see and work on that what is happening.

08:28.810 --> 08:30.100
We have gone through with that.

08:30.100 --> 08:33.850
So it has got my name, it has got the Google ID and it has got the email.

08:33.910 --> 08:37.840
The reason why we are not able to access this, it is because of the cookie session.

08:37.840 --> 08:40.300
But this is all good and this is all okay and fine.

08:40.630 --> 08:46.060
Now, at this point, we can actually go ahead and just stop this application here in the callback.

08:46.060 --> 08:48.940
If it is not having this all of this thing, that is fine.

08:48.940 --> 08:50.050
That is fine to us.

08:50.470 --> 08:52.360
Now notice here inside this password.

08:52.360 --> 08:55.930
JS We can actually go ahead and close this application right up here.

08:55.930 --> 09:01.450
Once we have sent this next, we can actually in theory call up our simple cookie token.

09:01.450 --> 09:03.970
Now, in case you are wondering, what is this cookie token?

09:03.970 --> 09:08.110
That is, you have to watch this entire t shirt store that we worked on.

09:08.110 --> 09:12.850
We can go ahead and create our own cookie token just like this, and I can go ahead and copy this and

09:12.850 --> 09:15.070
in fact, can make a cookie token up here as well.

09:15.130 --> 09:20.470
But the problem is there is a little bit more that Passport G does for you and for that these cookie

09:20.470 --> 09:21.850
session actually works on.

09:21.850 --> 09:24.490
So there are two more things we have to study for the password.

09:24.490 --> 09:29.080
This is all done in case you want to, but since you are using the password, we need to go one step

09:29.080 --> 09:29.680
further.

09:29.680 --> 09:33.970
There is a concept of serialization and serialization that happens in the password.

09:33.970 --> 09:38.200
Just a couple of lines of code, nothing too much, but you will understand quite in depth about it.

09:38.230 --> 09:40.240
So let's go ahead, move on to the next video.

09:40.240 --> 09:45.400
And since these are making entries in our database now, let's finally get everything, every error,

09:45.400 --> 09:50.650
clean up and have a good, simple application which helps us to log in with the Google.

09:50.680 --> 09:52.660
Let's go ahead and catch up in the next video.
