Hey did everyone had this year?
And let's move forward in this video.
In this video, we'll be touching a little bit of the database.
First, we need to fix that unauthorized and have to understand what's actually happening in the callback.
So one of the thing which passport does, among many other things is if I look at the passport JRS notice
here we are receiving this callback.
So we have this callback all going in.
We have access, token refresh and everything.
The profile is coming in.
Now, not only that, all that information that is coming in into your object, that actually goes a
little bit further down the road.
So let me walk you through what is coming in.
So I'll go into this odd dodges and instead of saying that this one is going all in, let me go ahead
and remove this and I'm going to go ahead and say request dot user.
You might be saying, hey, we don't have any middleware, we don't have ID, JSON token, we haven't
injected anything.
So how this request to use user is going to come in.
It will come in thanks to the courtesy of our password, guys, but not right now.
Let me go ahead and save this one and show you again that some of the stuff that is not going to work
again one more time, not how we expect them to work.
So let me go ahead and refresh that.
I'm going to go ahead and say, hey, this time, let's go ahead and log in Google.
Same login we go up here, it takes a little bit time and it says unauthorized.
Now the reason why it is saying unauthorized, because we haven't actually take the full advantage of
the password yet because honestly, this is not supposed to be worked that fast.
So let's go ahead and work on with that.
Now notice here, I'll come back onto this part that, hey, right now we were able to see nothing at
all.
We haven't written any middleware, we haven't injected any value, but we'll come on to this part.
First thing is now I have this profile that means I can access the user's email by simply saying profile,
underscore JSON, email.
So that is one clear.
Now before I move ahead and go ahead and say, hey, just go ahead.
And next, I would like to add an entry in my database.
So I'll bring in my user model and I'll say that search for this email into my database if it doesn't
exist, make an entry in the database.
If it exists, just allow the user to further move on.
Let's go ahead and work on that how this is going to work.
So I have this Google strategy and passport.
Let's go ahead at the top of it.
Let's bring the user as well.
So let's go ahead and say, hey, I want to bring in a user.
That user will be coming up from require and let's go one directory back inside the model and there
we go user.
So this user is up here and now I can access this one.
So again, by the way, just to remind you again, this is again up here so I can go ahead and say underscore
Jason and dot email.
I can access that again.
Better option would be in this case to access this through the emails.
But yeah, this is pretty much consistent, no such big issue.
So this one is going to be profile, dot, underscore Jason and dot email.
So this is how this is going to come in.
Okay.
Further down the road before I go ahead and do all these stuff, I will ask my database that, hey,
go ahead and find one user for me.
So let's go up here.
I don't have a big promise wrap around, so I have no much offer option.
I have to use them and catch quite a lot.
So I'm going to say, Hey model, just go ahead and please find one for me and you'll be searching based
on the email.
In the email, I'm going to go ahead and provide profile dot underscore Jason Dot email.
So this will give you the email now if the user exists.
So we're going to go ahead and say dot then and we can actually go ahead and say that if the then is
going to exist, then it is going to exist based on the user.
So it will return us that user.
There we go.
Now let's go ahead and quickly check if the user exists or not.
Now, if the user exists, that means user was present in the database.
So I'm going to go ahead and simply do a console log.
So log if it can suggest me.
There we go, console log and we're going to go ahead and say user already exists in DB and let's also
throw up this user whatever we have bring in.
So this is the user.
Now in that case, that means I'm all done and things are working fine.
So I'm going to go ahead and notice this time.
Very, very importantly, we haven't done this in the past.
I'm going to use this next.
Now, this next is little bit special.
In the case of Passport, the first parameter it takes is null, and the second parameter that it takes
is the user.
Whatever the user you have, you are giving it to this.
In this case, I'm giving it the user from my database itself.
Previously we haven't done this.
Next, that is fine.
We'll work on with that part as well.
Right now I just want to comment this.
We won't be using that part.
Okay.
So this is the if case.
If I have actually gone ahead and found the user, what about if I haven't found the user?
And again, we need to bring this cut it out and bring it up in the if case.
There we go.
So if user exist, go ahead and pass on this.
We have used this next a little bit differently.
This time we are passing on null.
This is how the passport is designed.
It's not nothing to do with the express itself.
And the second value that I'm passing on is the user itself.
Now, in the case, if we haven't found the user, then in that case, what we really want to do, we
want to create an entry in the database.
So let's go ahead and say user, dot, create and in the create part, let's go ahead and pass on the
object.
So we have a name.
The name is going to come up from a profile dot display.
Let me go ahead and check that.
So where is the name?
There we go.
Display name.
So display name.
Let's go ahead and make a copy of this one.
And inside our model, we call this one as Google ID like that.
So let's go up here.
And this one is Google ID.
So this is profile dot ID, all lowercase.
And then we have finally the e mail.
So let's go ahead and use that.
And in fact, I can just copy this from here and paste it up here.
So there we go.
Now there is a new entry in my database.
If this entry is successful, we can chain on a dot.
Then I know some of you don't like this much of chaining of the things, but let's go ahead and work
on with that and we're going to have a dot catch just like that.
In that case, if we receive an error, we're going to go ahead and say, hey, console dot, log this
error and in case everything goes fine, then that means this will give us a user back.
So let's go ahead and take this user work like that.
In that case, I'll print out a console, log on the screen.
So I'm going to go ahead and say log
user.
Let's call this one as simply new user and also throw back that user.
Now, most importantly, just like we have used the next here, also we have to use the next and the
first parameter is null.
This is actually an error.
So we expect we won't be handling any error in this case and we're going to go ahead and pass on this
user.
Now, the most important thing, what is going to happen when we pass on this user?
Previously we were facing this unauthorized.
That was because behind the scene this password goes in and tries to work the middleware that we have
approached in the last section, remember, we were having this ID, we were creating this JSON token,
then we were kind of demystifying or extracting the values from the token, the ID itself, creating
our database, and we were putting all these things into request user.
This is so much of a standard practice that passport GC also does the exact same for you.
So when you say next and you pass on this user next year, this passport will automatically come in.
We'll create a property user so you can access that by request dot user and we'll add on all the properties
here.
And this is exactly the reason why I have told you here that request user So just like we were expecting
that in the t shirt store that hey, we were doing everything manually, we don't have to do this this
time.
Passport guys will take care of the things for us.
Okay, so this is quite a lot that we have worked on, so let's go ahead and try this time.
So let's go into the database, hit refresh.
We have passport and the user right now, there is nothing inside the user.
Let's go back onto the screen and see if it is working or not.
So again, localhost and let's go back up here.
So this is our login screen.
Let's go into the Google and again my email and let's go ahead work on with that and it says fail to
serialize user into the session.
Now again remember we have this cookie session.
Yes, that is the reason why it is failing.
But in theory, let's go ahead and see and work on that what is happening.
We have gone through with that.
So it has got my name, it has got the Google ID and it has got the email.
The reason why we are not able to access this, it is because of the cookie session.
But this is all good and this is all okay and fine.
Now, at this point, we can actually go ahead and just stop this application here in the callback.
If it is not having this all of this thing, that is fine.
That is fine to us.
Now notice here inside this password.
JS We can actually go ahead and close this application right up here.
Once we have sent this next, we can actually in theory call up our simple cookie token.
Now, in case you are wondering, what is this cookie token?
That is, you have to watch this entire t shirt store that we worked on.
We can go ahead and create our own cookie token just like this, and I can go ahead and copy this and
in fact, can make a cookie token up here as well.
But the problem is there is a little bit more that Passport G does for you and for that these cookie
session actually works on.
So there are two more things we have to study for the password.
This is all done in case you want to, but since you are using the password, we need to go one step
further.
There is a concept of serialization and serialization that happens in the password.
Just a couple of lines of code, nothing too much, but you will understand quite in depth about it.
So let's go ahead, move on to the next video.
And since these are making entries in our database now, let's finally get everything, every error,
clean up and have a good, simple application which helps us to log in with the Google.
Let's go ahead and catch up in the next video.