We've now got our minimum amount of configuration put together for our Google strategy as the first
argument we passed in some configuration options and then as the second argument we placed an arrow
function as a Colback and we're not really too worried about what this is doing just yet.
But we will talk about it in just a little bit.
Now we've basically got enough configuration put together here to test out our authentication flow.
So in this section we're going to do one last little small change and then we're going to start to test
out the full authentication flow.
Now before we do that I want to review our flow here very quickly.
Remember that we had said that whenever a user clicks our logon button they are going to be directed
to our server slash.
Slash Google.
Our application will then for them onto Google servers and after the user grants permission to our application
they will be kicked back to our server specifically to the route of local host Colin 5000 slash off
slash Google slash callback.
So the last thing we have to do to test this flow out is make sure that we put together some configuration
to say hey whenever a user goes to slash or slash Google we should then start the entire process being
managed by passport.
So thats the last little piece we have to put together.
Remember how we had said that we use Express to do different things based on what route a user visits
we put together a route handler.
And so thats exactly what were going to do right now.
We're going to put together a little route handler inside of our application that makes sure that the
user gets kicked into this passport flow.
All right.
I mean to change back over to my next guest file and then directly underneath the passport you statement
we're going to add a single route handler.
Now remember we had spoken about rope handlers Previously we had said we referenced the Express app
object we state the type of or method of the requests that we want to handle here.
And so in this case we want to handle a get type HTP request.
Then the first argument is the path that we want to handle which in this case is slash off slash Google.
And then the second argument is some code to be executed whenever a request comes in to this route with
this request type.
Now in the past on the previous example we did we put an arrow function in here that was executed whenever
a user came to this route.
But this time around as a second argument we're going to tell express to involve passport and basically
pass the user off to passport where they will then be kicked into this authentication flow.
So it's the second argument we're going to say passport Daut authenticate we're going to give it a first
argument of Google as a string and then a second argument that's going to be an object.
It's going to have a property scope and we'll talk about what this is in just a second.
And that will be an array with profile and email like so OK.
So let's save this.
And we're going to talk about what's going on here.
So again whenever user it comes to this route we want to kick them into our waterflow which is being
entirely managed by passport.
So we are saying hey passport attempt to authenticate the user who is coming in on this route and use
the strategy called Google.
Now one quick thing that is very confusing with Google with passport and remember this is one of the
other complaints they had about Google is that some of the configuration you do it's kind of like whoa
what's going on here.
This is kind of weird.
So I want to specifically point at this string right here.
Google.
So when you read google you might think OK.
Yeah.
You know that makes sense.
We want to authenticate the user with Google.
We want to use Google Earth.
However if you look back up at all this configuration that we did right here we had never explicitly
said Hey here is this Google strategy and it is called like the string Google.
In other words we never specifically registered the strategy with passport and called it with a string
of Google.
So we never set up this identifier right here.
So here's what's going on internally.
Google's strategy has some little bit of code that says I am known as a strategy called Google like
the string Google.
So hey passport when you load me up if anyone attempts to authenticate and say authenticate with a string
of Google use me use this strategy right here.
OK.
So again the Google strategy has an internal identifier of Google and that's how passport knows to take
the string right here and go and find this strategy that has been already wired up to it.
OK.
So that's the first argument.
Now the second argument is an options object and we passed in scope right here.
The scope specifies to Google like the actual Google servers what access we want to have inside of this
user's profile.
So in other words we are asking Google to give us access to this user's profile information and their
e-mail address as well.
These two strings that I passed in here are not randomly made up.
So Google internally has a list of all the different scopes or permissions that we can ask for from
a user's account so we can also ask for say the ability to read a user's contact list or read all the
images inside of their Google Drive account or read all of their e-mails.
Those are different scopes that we can ask for.
And side of the process in this case we are just getting access to the profile in e-mail.
I don't really care about any of these for our application like they don't really make a difference
for our application.
I just want to give you an example of how we can ask for very specific pieces of a user's account.
OK.
So I think they are now ready to test this out inside the browser.
We have set up our handler.
We've said that when ever someone visits the route or slash Google they should be directed into the
palace for authentication flow.
So I want to now test this out.
I'm going to tell you right now that when we test this we're going to get a very small error and that's
totally OK it's because I want to draw like very specific attention to this one aspect of authentication
or lot in general.
OK.
So we're going to get there but that's totally OK.
We're going to fix it up very quickly.
So let's first make sure that our servers running to do so will change back over to our terminal.
I'm still inside my server directory and I'll start up the server with node index dot J.S. and that's
going to start the server up then I'm going to open up a new browser tab and remember to get Flash Google
on our server which is running on localhost Colan 5000.
So we're going to say look a close call and 5000 slash off slash Google.
Now when we go there
Express is going to see that we are making a request to Google.
It's going to access that route Candler that we just put together and it's going to direct our request
into this big old passport flap.
So let's get to it.
Let's see what happens.
So hit enter and as you can see we successfully got redirected over to Google.
So like something good happened here like we are clearly at Google we are doing something with a cloth.
But very quickly you'll notice that we've got a nice big error message on the screen with an arrow that
says redirect you are I mismatch.
And so this is a very important air to see right here because this is something that you're very likely
to see whenever you are setting up a life for yourself.
And it also will give you a little bit more insight into what is going on under the hood with a laugh.
So let's take a quick break and when we come back we're going to fix up this error message and then
test out our application again.