All right, before we go any further, let's take care of this one to do item, we want to take the
user back to the login screen.
If things didn't work the way we expected or if the username and password don't match what's in our
database, whatever the case may be, if the form is not valid, we're going back.
So how do we do that?
Really simple.
We've done it before, rendered a template, the response writer, our pointer to our request, the
name of the template, which in our case is login page dot temple and a reference to a model's template
data.
And all we have to populate is our form with our form information.
Now back in our login page, of course, we want to be able to test this.
So we'll put new validated in here right now and back in our handlers after we've rendered that we want
to return.
We don't want to process any further in the application.
So if we've done this properly and I think we have, we should be able to stop our application.
If it's running, start the application.
Go back to our Web browser, reload this page and try to submitting nothing, and it says this field.
That's exactly what we wanted.
So you notice here this actually should be an email.
So there's one more check we should put in here, which is form DOT is email and the field is email.
We may also do that.
Let's stop.
That started again.
Make sure that works.
It will.
But I like to validate things and we'll put in something that's not an email and we'll put a password
in this time.
Invalid email address.
Perfect.
OK.
And of course, it's trying to save my password.
I have to disable that at some point.
Anyway, that seems to be working.
All right.
So validations taking care of what's our next step?
Well, our next step is after we've successfully logged this user in, we've stored this user ID in
the session.
And what we want to do now is to write some middleware and possibly a helper function to determine whether
or not someone is logged in.
So let's go over to our Help Helpers file to start with, which is I'll just find it this way.
Helper's ago.
There it is.
And I'm going to create a new function, which I'm going to call is authenticated
and it's going to take what.
Well pretty straightforward.
It's going to take the request which is a pointer to an HDP request
and it's going to return a boolean.
True.
If someone is authenticated, false if they are not and it's going to be as simple as this exists equals
app dot session dot exists.
And then it needs the context and context which is passed as a parameter, this function and that I'm
looking for a particular entry in that.
And that entry is user underscore ID.
So I somehow deleted that closing parentheses and then I just returned exists.
And that's it.
So there's a function in my helper that I can always call from anywhere, I have access to the helpers
to see if someone is authenticated.
Now let's open our middleware.
So let's find our middleware right here and let's write some new middleware.
Now, I call this middleware off just as simple as that, and like all middleware, it'll take up the
parameter of next, which is htp dot handler type and it will return and htp htp dot handler as well.
Now, one thing that's different about this middleware and say this one or this one is that here I actually
need to have access to the request because in this middleware I'm going to call that helper function
is authenticated that we just created and it requires a pointer to the request as a parameter.
And we can get that and we can get that by doing something a little bit differently in this middleware.
We're going to have a return.
So this is going to return an active handler.
So what how am I going to get that handler?
By calling the handler funk and having as its parameter an anonymous function which will be of named
funk because it's just that anonymous function and it will take as parameters w SCDP DOT response writer
and R, which is a pointer to HTP request.
And that's what I wanted.
And I can do that because I'm returning something that has access to the the response operator, response
operator and the request from the HDB handler func.
So I've now done this and inside of that the body of this function is where I can actually call helper's
is authenticated, if not Helper's dot is authenticated and pass it that request a pointer to the HP
request.
Then if they're not authenticated, what I want them, what do I want to do.
Well I need to put an error message in their session dot put and I'm going to put in the session from
our context because the session needs that and I'll put an error.
And I'll put in the message log in first, and then after that, I'll do a redirect, SCDP redirect.
W are, and I'm going to redirect them to the login screen user login Ishido status, see other.
And at that point, I will return.
I don't want anything else to happen, so this middleware is something I can apply to routes that I
want to protect.
Now, of course, I can't stop there.
I need to actually do the next DOT serve HTP and put it in the w our like we do with all good middleware.
So what this is doing, this is this is exactly the same logic as the no surf or as the session load,
but it's our own custom middleware that actually has access to the request.
So we can call helper's dot is authenticated and if this doesn't fail then we just pass onto the next
middleware if any, and the request lifecycle continues on its merry way.
So this is going to be incredibly useful because we can use this middleware to protect roots in our
roots file to ensure that only people who are logged in actually have access to the roots that we want
to protect.
So we've got this built and we will take care of implementing it in the next lecture.