So let's continue working on the authentication process and right now, I mean, Handler's dash appeared,
I'd go and I'm looking at the creator of Token Handler and all we've done so far is to declare a variable
here, user input, read the information into that using our region function and send back the bad requests
if things go wrong.
And then we've hardcoded a response down here that just says things worked as expected.
And of course, all of this stuff in the middle is what we need to do.
And as you can see, you have added a few comments here.
And what we want to do is after we've got the input from the user, where we have their email and whatever
password they've entered, we want to get that user or at least try to get that user from the database
by email address and then send back some kind of error message.
If the email is invalid, if the email is valid, the next step is to validate that password, to compare
what they entered against the hash we have stored in the database and again, send an error back if
there's an invalid password, if both of those things pass at that point, we want to generate the token
we're going to send back to them and send that back as a response.
So there's a few things we have to do before we can get too much further.
So let's get started with this, getting the user from the database by email.
So I'm going to go over to models DOT go and I'll just scroll the very bottom and I'll put a database
method in here that allows us to look up a user by email address.
And I'll call this func with a receiver of M.
D model like everything else in this package, and we'll call it and get user by email, which seems
logical.
And we're going to take one parameter which we'll call email, and it's a string and we're going to
return potentially a user if we can find the user and potentially an error if we cannot.
So we'll use our standard context, logic, which I'll just copy from the insert customer because it's
exactly the same and it will save some typing and paste that in here.
And now I will actually take the email address that we've been handed and I'm going to convert that
to lowercase because I'm only going to store email addresses in lowercase and sometimes people entered
in the wrong way.
So we'll use the strings package and we'll use that to lower function and convert email to lowercase.
So now we have a valid email address we can use to look things up.
So we'll create a variable to store a result for you as user.
And I will query the database.
So we're looking for a row.
That's what we're getting back from DB query row context right there.
And we had it the context and then we handed our query, which I'll just put right in here because it's
pretty short.
So I'll use back.
So I have lots of room to type things.
And the queries just select, which you have to spell, right, we're going to select Idy first name,
last name, email password created at an update from users where email equals questionmark and I'll
move this back, take up on the same line just so things are a little more attractive.
And of course, our substitution is what we received email.
So there's our call to the database now, we'll try to scan that into our variable, there is a sign
the value of rodents can.
Give ourselves more room and we're going to scan this into user, don't you, ID, and then we'll just
duplicate this a few times.
First name.
Last name, email.
Password
created
and updated, and we check for an error, if error is not equal to nil,
return you and the error.
Otherwise return, you know, and let's give this a comment,
gets a user by email address.
So we have that function available to us, that means we can go back to Handler's Dash API and actually
try to get the user so user an error or assign the value of DB, get user by email and we're handing
it user input email.
Now, of course, at this point we want to check for an error.
So if error is not equal to nil, we want to send a response back.
And the response we want to send back is that this is invalid credentials.
You didn't give me valid credentials.
And in fact, that's the kind of status code we want to send back.
Now, this is the sort of thing we might be doing fairly often, authenticating users, making sure
that we have a valid username and password, whatever the case may be, we might do this more than once.
So it seems to me it might be useful to go over to help restart, go and to write some kind of function,
maybe invalid credentials.
And then it's very easy for us to send back that message, invalid credentials.
So let's take care of that and we'll do that in the next lecture.