So let's use verifyAuth to protect pages.
And here in this basic demo app,
I have just one page that should be protected.
But of course you could now use this verifyAuth function
in any page, or in any API route,
whatever it is that needs to be protected.
So here it's that training page,
where I will call verifyAuth before I do anything else.
Because I don't wanna fetch training data
if the user is not logged in.
So that's the first thing I'll call.
Now we need to await this because that returns a promise,
and as you learned,
we'll get back an object here from verifyAuth,
a result object, which has a user key and a session key.
Though both user and session may be null
if we did not find a valid session for the user.
So here we can check if not result.user,
so if that result object contains no user data,
in which case we know
that the request is coming from an unauthenticated user.
And in that case,
I wanna return here and not execute any other code.
I don't wanna fetch training data,
I don't wanna render and send back that page.
Instead, I want to redirect the user
and send them to a different page.
We can do that with help of the redirect function
provided by next navigation.
And I wanna send the user to the starting page here
in this case, so that they can't visit this page.
Whenever they try, if they're not logged in,
they will be sent back to the starting page.
Now keep in mind, here, in my case, I did log in before.
So if I save everything and I go back
and I reload that training page, that works.
Here, if I click that reload icon,
I can visit this page as often as I want.
But that will now change if I delete
that authentication cookie.
So if I go to the application tab and the dev tools
and there I select this cookie, if I delete it,
and I then try to reload, you see,
I'm redirected back to that starting page.
Because no valid authentication cookie
was attached to the request.
And that's the offer.
What we actually want to do automatically,
if the user clicks a logout button,
which we don't have on this page yet, we'll add one soon.
But before we do that,
I wanna make sure that we can actually log in
because thus far, we only support user creation,
and that's of course not all we need.
Instead, I also wanna allow users
to log in with existing accounts,
and that's what we'll do next.