Hey, did everyone that they shared.
And in this video, we're going to work on the forgot password so we know that how we can send the email.
So the idea is if somebody visits the slash forgot password, he will send us the email in the body
or the JSON format.
And on that email we'll be shooting up an email based on we have we are able to find the user or not
and the email is going to be a little bit crafted like this slash password, slash reset and then we'll
be able to grab the token.
So we'll be working on just the forgot password in this video.
And in the next one we'll be working on the forgot password section.
Again, this is fairly, fairly simple.
Once you see me walking through that, how this is being done, you'll be able to do it for any of the
application coming up to future into your place so we can close this email helper.
We'll come back onto this later.
Let's go into the user controller.
So what we're going to do is let's go ahead and kind of copy a method so that we have this boilerplate
code, in fact, duplicate that.
And we're going to be calling this one as simply forgot password, forgot password in case anything
else makes sense to you.
Go ahead and simply go ahead and use that.
Okay.
So again, we have got sign up, log in and this log out.
So we have actually changed this one.
This one.
I preferably like to cut it out and go at the end of it.
So again, doesn't really matter, doesn't really bother.
Okay, let's go ahead and remove this.
The first step is to actually extract the email.
So let's go ahead and say, hey, I would like to grab the email.
So this is how the email will be coming up and we'll be collecting it from request body.
Okay.
Now I have this access of email.
The first thing that I would like to do is check whether this email exists into my database or not.
And for that really simple user dot, find one.
There we go.
And I can go ahead and pass on this email directly here.
If, if this, this email exists, I would like to hold it into a variable.
So I'm going to go ahead and call this one as simply user.
And since this is an operation which is happening on another continent, obviously, I'll go ahead and
wait for a minute.
Now, coming up on to the user model.
We have designed a couple of methods specifically this get password token.
A couple of things very interesting here that notice it is definitely getting us this forgot token so
I can run this method and get the token itself.
It is also setting up the expiry, but it is not saving anything in the database.
Very, very crucial.
And you don't save things right into these methods.
Whoever is using these methods needs or have a responsibility to save them.
So let's say that we want to use this.
Get password token, get forgot password token.
Let's go ahead and use that.
So we're going to go ahead and say that this is my for forgot token and how we are going to access that
remember small you user because that's the object and we're going to go ahead and paste that.
Now this has kind of initiated a process in which the token is being given to me, but also this forgot
password expiry time as also being saved up here, but it hasn't been saved onto the database yet.
So how we are going to do that?
We need to first work on this user and we have to simply say dot save.
Now, in theory, in most of the cases you'll see that this actually works most of the time.
But sometimes your model is designed in such a way that you are saving the things it requires some of
the compulsory field at the saving of the time, and we are not passing all of them, we are just passing
two of them.
So in some cases you can go ahead and simply say that, hey, I want to simply say to you that, hey,
go ahead and validate that.
So validate before save.
I can go ahead and turn on this flag as false.
Make sure you write that carefully before save.
Yeah, looks great.
So what it's going to do, it's temporarily going to just not check everything and we'll save the data
however you are asking it to save again.
We are doing this because we know exactly what we are doing and that's why this flag is being used.
Usually people don't like to use it.
I including me again, I told you this all operation happens on another continent, so make sure you
await there.
Okay?
So this is good.
Now I have a forgot token.
All I have to do my database job is all done thanks to the methods that I've designed earlier.
Now the responsibility is that I need to send this forgot token to the user and not just to the user.
Because if I go ahead and look on to this, any email, this is not going to be simply slash forgot
password and user knows automatically that I have to say localhost colon 4000 or learn code online something
it doesn't really know.
So we need to craft a URL for this particular kind of a string slash password slash reset and then colon
token.
So let's go ahead and craft a URL for that.
So let's go ahead and say simply my URL, my URL, feel free to name it anything.
It doesn't really matter.
We're going to use the back takes up here because obviously we are going to use some of the variables
up here.
The first thing we're going to go ahead is request protocol.
Now, this is.
To verify that whether you have a CTP or a CTB is the same thing.
We talked about the documentation exactly same.
Then we have Collins slash slash and then we have to get the host name as well.
So let's inject another variable up here and we're going to go ahead and say request dot get and inside
the guest we are going to go ahead and say host.
Yeah, it comes up in the double code like that.
Okay, so this is all done.
Now you have got the host name as well, whether it is local host Colon 4000 or it is learn code online,
whatever it is, it's going to come in.
Now, on top of that, you are putting up a slash.
Remember, again, we are trying to craft this URL, slash password, slash reset and the colon token.
So we're going to say slash password, slash, reset, feel free to convert it into however you like,
but make sure you remember it because obviously later on we will grab this token from this exact URL.
So after that, we are going to go ahead and inject another variable that is going to be simply forgot
token.
Okay.
So now this entirety of the token and everything is all done, this is all good.
Now only thing remaining up is to shoot an email and send this one to the user, whoever is requesting
this one.
So let's craft a message based on this URL.
So we're going to go ahead and say, Hey, this is my message and this message is going to be something
like this.
As of now, we are saying copy paste this link in your URL and hit enter and then we can use a couple
of slash lines so that it gives us a new line.
And all we are going to go ahead and do is inject a variable.
So let's go ahead and inject available.
That is my URL.
Come on, suggest me.
There we go.
So this is our really crafty message.
Surely you can go ahead and make it more beautiful and stuff, but I'm not interested in that.
Now, one more thing interesting up here, we have crafted this message, but now it's only remaining
is to send an email.
Now sending the email is really, really tricky.
One, it can fail more often than you can think for.
So that's why I always like to wrap these email stuff into Try and catch block.
Interestingly, if the email goes nicely, that is all going to come in the try catch.
But the most important or the learning part is the catch.
What if the email is not properly sent?
Then things goes absolutely crazy wrong.
Because if you remember in the models we have actually set these fields which are these forgot password
token and forgot password expiry.
Next time user comes some and he is going to get these filled up.
That is not good.
These fields should be emptied out.
So what we're going to do is we're going to go ahead and take those fields.
So let's go ahead and take this one.
So this user has to field.
I would like to grab it from here.
So this token, go ahead and place this one and there is another one which is forgot password expiry.
These fields need to be flushed out.
How do we flush them out or kind of make them empty?
We have already seen that in the past, just like we do in the password field.
So we go ahead and simply say, hey, this is going to be undefined and this is also going to be undefined.
Now obviously you have just undefined it.
You haven't saved it.
So to save this, we're going to use again the same line that we just wrote again with validate before
Save.
So let's go ahead and do that.
And that's it.
That's all you got to do to save this one.
But that's not all about it.
We need to return an error here.
So we're going to go ahead and say, hey, if anything goes wrong, let's go ahead and wrap it up with
the next.
We're going to go ahead and handle it with the custom errors.
And we're going to simply say that, again, we are receiving this error here so we can go ahead and
simply just dump the message here.
So we're going to go ahead and say error message.
Surely errors can be handled a little bit more beautiful than this, but we're going to go ahead and
throw probably 500 here.
So again, this is the most interesting part up here.
And a lot of interviewers are actually worried about this part because anybody can handle the good part.
The bad part is what you need to worry about.
Okay.
Let's go ahead and see that how we are going to send this email.
So I'm going to go ahead and simply use a wait because email takes time always.
And then we are going to use the email helper.
So I'm going to go ahead and say, hey, email helper, that's not going to come on automatically.
Are we exporting that even?
Yeah, I'm exporting mail helper, so copy that.
And it is not email help but it is mail helper.
Come on.
There we go.
Male Helper I love that.
That it automatically imports that for me.
I love that thing.
Again, in case yours is not working.
Go ahead and simply just add the line there.
Okay.
So this is the male helper, but male helper expects you to pass on an object.
And inside the object we expect email, subject and at least the message so we can craft an object and
store that in available or we can just go ahead and do like this.
So email, subject and message, let's go ahead and give it that.
So first field is email.
So email is going to be simply whatever the user dot email notice we can use the user dot email field
because user obviously saved or we can use this email.
Kind of a same.
We have already verified these things throughout here, so this is all what we got.
Now we haven't taken care of the situation.
What happens when we don't find the user?
So yeah, that needs to be take care.
Reminds me.
Okay, so let's go ahead and say if I don't find a user, I'm going going to go ahead and say return
next and we're going to say new custom error and we're going to say email not found.
As a registered.
And we're going to pass on a simple 400 error.
That's it.
Okay.
So we have already verified now 100% sure.
And now here onto this part, we can go ahead and pass on either user dot email or whatever the email
you got.
Okay.
Next one is subject.
So let's go ahead and say, hey, I want to pass on a subject.
This one is going to be hardcoded string.
Let's call this one as password reset email.
And again, I would like to add the company name here.
So we're going to say ae t store.
Yeah, these two or t shirt, whatever you like to go ahead and add that.
And the last is message.
So we're going to go ahead and simply say that message.
Again, you can say message, call a message or we can just use it like that.
Okay, let's see one more time.
So two, which is an email subject and message, hopefully it looks good and the things seems okay,
things seems okay.
Now all we got to do is send an email as a request body into this URL forgot password.
Let's go ahead and set up a route for that.
So we're going to go ahead and say, hey, now I have another route which is forgot password and let's
see how we have designed it up here.
It says Just the forgot password.
Really nice.
And we're going to go ahead and say this is.
Forgot password and this is also being handled by forgot password.
This obviously is a post route OC so seems like all okay and all good.
Let's go ahead and test it out.
Whether things are working or not coming back up here, let's just say I forgot the password of two.
So let's go ahead and save this one as save as this one is going to be forgot password.
Save that this one is a post route and save that forgot password and we need to send something into
the body.
And this time we don't need to send the email, we just need to send the password again.
No comma is required.
Save that and let's go ahead and try to send this one.
Are we sending any JSON response?
Looks like we're not sending any JSON response.
Let me just quickly send that.
So we are sending the try in the email, but user has no idea whether the email got or not.
So let's go ahead and say restart status, which is going to be 200 and then chain on a JSON response.
This might be looking very, very familiar to you now.
And we're going to go ahead and say, hey, success was true and the message is going to be email,
send, send success fully.
So these are all the messages that we got.
Seems OC seems good.
Let's go ahead and send this one and it says, hey, error, the option is not defined.
Let's go ahead and see that where the option has not defined.
So in line 121, after stating it for a little bit while, I quickly realized that this is option and
this is all going with the option.
So it is better to just go ahead and say that this is now option should be all good now.
Test number two, let's go ahead and try to send this email one more time.
Send this one.
And this time it's taking a little bit while to send the request.
And the regret says success through email send successfully told you database is always on another continent.
Let's go ahead and see if we are receiving any of the mail into our mail trap.
Let's go ahead and hit refresh.
And there we go.
Now we have five.
Okay.
Previously it was just four.
Let's go ahead.
And this is the new t shirt store.
So this is how your email are going to look like.
And this is the text we have.
So http localhost, 4000 password reset and the token is there as well.
So this token is valid only for 30 minutes.
So obviously now we need to work on this password reset token.
Now all we got to do here is really simple.
Telling you in advance we will visit this URL.
We'll grab the token from the URL.
Yeah, you got it.
Request dot params.
I know you are absolutely pro now and once we have grabbed this token we have to match whether the token
is matching with our database token.
Really simple stuff.
If they matches out and expire is also good, then we will allow him to update the password and we'll
take the password and the body will take just the password field either or we can take the password
and confirm password whatever suits to you and whatever is your flow.
Let's go ahead and catch up in the next video.