In the last section we did an overview of the passport J.S. library and we got a sort of sense of how
it might help us out with authentication with Google inside of our out in this section.
We're now going to continue by installing passport and installing the passport Google law strategy.
We'll then start to wire them up inside of our application and we'll get a better sense of exactly how
they work.
So to start off we're going to make sure that we install passports into our project.
Now we're going to install many different modules into our project over time every single time we do.
We're always going to be using the command line and we'll be making use of the NPM package manager.
So inside of my server directory I'm going to run the command NPM install dash dash save passport.
So that's our base passport module member that's the generic set of helpers that makes passport work
well with Express.
And then we're also going to install one strategy called Passport dash Google dash off 20.
Now you see the 20 on here you might be kind of scratching your head and saying did he mean to say 20.
And yes I did.
Make sure you put passport google oh off 20 like so.
So let's install this.
Hit enter let it do its thing and while that runs I'm going to tell you a little bit about why we're
using this last 20 thing really quickly because you might be curious.
If I change back over to the passport documentation which I still have open I'm going to open up the
strategy section and I'll search for Google.
Now when I do that you're going to see on here Google off authentication strategy over here.
And so that kind of seems more relevant.
And the name of that library is Passport Google off.
I encourage you to click on the saying or just follow along.
It really doesn't matter.
And once you get here you'll go to the get hub page around this module.
If you scroll down a little bit you'll notice this first paragraph right here.
This paragraph essentially says that the passport Google module only works or only exists for backwards
compatibility and it actually recommends that you use either the passport Google Auth. one or a passport
Google allow 20 modules.
And so that's why we were using about 20.
So we can click on this 20 link right here and then we'll get some documentation around to how to set
up this strategy inside of our application.
And so if you want to you can read a little bit about usage down here but believe me we're going to
go through all these different steps and great great detail.
Now just in case you're curious why is it called 0.20.
Well 20 is short for 2.00 or aloft 2.0.
You can't put dots inside of NPM module named names.
And so the library author decided to say or 20 instead of just like a watch 2 or something like that.
Anyways we've now got them all off.
Insall inside of our application.
So let's change back over to our code editor and start to put this thing together.
Now I'm going to tell you that for this very first authentication feature that we're going to work on
we're going to just go ahead and place all of our code directly into his index file.
So we've got one single location where we can see all of this authentication logic.
But as soon as we finish this feature or maybe even when we get like halfway through it we're going
to very quickly come back and refactor this file into several separate files inside of our codebase.
OK so first feature everything inside of indexed J.S. and then we're going to very quickly refactor
everything.
OK.
So let's get to it.
The first thing to do is remove the existing route handler that we had set up previously.
So the apt get forward slash.
That was really just a test route and we don't really need it anymore.
So I was going to delete it altogether.
Next I'm going to require in two separate modules right underneath the require express statement.
I'm going to first require in passports so I'll say require passport and I'll assign it to the variable
of passport and then I'm going to also import or require in the strategy that we just installed as well.
So I'll say Konst Google strategy is require passport Google.
Make sure we get to ASSS their passport Google along with 20 lifeso.
Now the passport Google 0.20 module actually exports a couple of different properties and we only care
about one very particular property which is the strategy property.
So we're going to add on Dot strategy and to the end like some OK.
So we now have passports which remember give's express the idea of how to handle authentication and
then the Google strategy which we use to instruct passport on exactly how to authenticate our users
with Google Earth OK.
So now we've got these two in here.
We're going to take our passport library and we're going to inform it or tell it that it should understand
how to make use of the Google strategy inside of our application.
So underneath our app declaration right here we're going to say passport dot use that's a function.
So we're going to put down our parentheses and then we're going to put down new Google strategy and
put a set of parentheses after that as well.
OK.
So already we've got some kind of weird looking code right here.
Let's start with what's going on inside of the function call new Google strategy creates a new instance
of the Google passport strategy.
So this essentially says hey application I want to somehow be able to authenticate my users with Google
and inside of this function right here inside of this constructor we're going to pass in some configuration
that tells this Google's strategy how to authenticate users inside of our application.
And all very shortly see exactly what options and what configuration we pass in here to configure it
for our out before we get there however I want to talk about the other function call that we just put
in of passport Daut use.
So passport use you can think of as kind of like a generic register something to say hey passport you
know how to handle authentication in general but you don't really know how to authenticate users with
a very specific service or a very specific provider.
So you can think of passport use as saying passport I want you to be aware that there is a new strategy
available and here it is make use of it.
Understand that users can use this to authenticate themselves inside of our application.
OK.
So I think that's enough right now.
Let's take a quick break.
When we come back we're going to add in some additional number of options to this Google strategy to
customize exactly how it behaves inside of our application.
So let's take a quick break and I'll see you in one second.