So let's keep working on our reset password page, and I'm looking right now at the bottom part, the
JavaScript, and what I need to work on next is the actual point from here on, after we get all our
validation, everything is OK.
We have two matching passwords and we need to send a payload.
Now, we're not able to get the email from the form, so we'll change that the password, because we
know that's what we have in the form and we'll call that password.
So that gets the password and the email we pass to this as data.
So email is actually equal to, in our case, index data.
And we called it email and we closed the double quotes there and closed the double quotes here and give
it a comma.
So now we have our payload.
And we're not going to go to forgot password.
We're going to go to reset password.
We get an error, we're still going to show the error.
But if we get success, I actually want to display just for a couple of seconds the success message
that says password has been reset and then I'll take them to the login page.
So I'll just say Satima.
And that will be a function
and all we're going to do in there is after two thousand milliseconds, which is two seconds will say
location, a trip.
It was the longest I can log in at that point.
So we've got this all set up now when you go to the back end and add a route for resetting the password.
So let's go to our API roots route, Stasch API.
Then right here, duplicate this line and change that to reset password.
And we'll call a function which we'll just call reset password,
which we have create now.
So let's go to our handlers API.
And right at the bottom, I'll add any function function with a receiver at the point of the application
reset password, give it its two required parameters.
OK, let's give ourselves some room and start writing this.
Now we know we're getting a Jason payload, so let's create a variable called payload and it's a struct
and it only has two fields.
Email, which is a string and an adjacent that's going to be called email and password.
The new password, which is a string.
And in JSON that's called password.
And of course, we just read our Jason, so we check for an error is Abdul-Karim Jason and it requires
W and R and we're going to read into our payload variable.
We just create it.
If there's an error,
we'll just say after that request and you can do whatever you want that are error and return.
Once we have the Jason, let's go get the user by email, so user and potentially there are assigned
the value of DB, don't get user by email and that would be payload email.
And again, we check for an hour, just copy and paste them and give ourselves some more room.
Now I'm going to use that user because I need to create a database function that resets a password with
user ID and that gets me the user I.D..
So now let's create a new hash.
We have the password and so let's create a new hash.
And this is really easy.
We just use the standard decrypt library decrypt and we want to generate a hash from a password.
And it requires two arguments.
The first one has to be a slice of bytes and it's the new password.
So that's payload password.
And the second one is just an integer.
How big a hash should this be?
And 12 is what we've been using right along.
So let's use 12 then.
That's kind of the standard for this sort of thing.
Again, if there's an error, I'll just paste in what was on my clipboard.
So now we have the new hash for the user, the new password hash, because we're not storing the password
in the database for storing the hash.
So what I'll do now is call a function that I haven't written yet after DV dot and I'm going to call
it update password for user.
And I will take two arguments.
A user and the new hash, new hash, which has to be a string.
So I'll just convert my hash to a string new hash and I'll check for an error.
Now this doesn't exist yet, so let's go over to models.
Go and just write it because it's a very, very simple function.
So models don't go at the very bottom.
I'll create a new function func with a receiver m DB model and it's going to be called update password
for user.
And it requires two arguments.
You, which is a user and hash, which is a string, potentially it returns an error.
So I return at the very end, then I'll go up to the top and copy my contact stuff as I always do.
And I will write my statement statement as TMT is a sign the value of update users set password.
I called a password even though we're not storing password because it's easy to find that way.
Equaled questionmark, where idee equals to questionmark and we just ignore the first return.
We still care about the result, but we do care about the error is a sign the value of Amla, GB, the
exact context, and we give it the context.
We give it the new password, which I called hash, and we give it the user, which is Utah ID, and
we check for an error.
If error is not equal to nil, I should have to give it.
Statement to CMT, that's better if air is not equal to no
return or otherwise return, which is already there, so that should take care of that.
So let's go back to our API handlers.
Temblors Dash API Duko.
We've now updated the password.
So at this point, all we have to do is send back a Jason response saying everything went as expected.
VAR RESP is a struct and it has an error which is a bool.
And in adjacent that's called error and it has a message which is a string which we're ignoring.
But I'll put it in there anyway.
Jason, message
rasped error equals false because there is no error.
Message is equal to password change and I just write to Jason after Jason give it w htp dot status created
just fine and respin.
And that should be it.
So let's try this, OK, let's make sure everything is stopped.
Make stop start.
And let's go back to our Web browser and let's click on Forgot Password admin at example dot com.
So it should have sent the email so ago to Mail Trap, and there it is, click on the link.
There's my password form, so my old password was password.
I'll make the new one password to password to reset the password.
Password reset.
And now I'm with the logging streets and I'll try logging in as an example, not with the old password
password log it.
Invalid authentication credentials.
Now I'll put it to at the end.
And I've logged in successfully, so we've managed to update the user's password, so that's it for
this part.
Let's move on to the next task.