تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Password Reset Overview0:00

Okay, let's take a look at resetting passwords. So this involves two steps. The first step is requesting a password reset link. And this is how the form looks in Jetstream. So let's go ahead and build this in our Fortify app here. So the first thing I'm going to do is put a forgot password link on this login page. Actually first we have to define the view for forgot password. So let's add this like we've been doing to our FortifyServiceProvider. So let's go to that, let's put that here, and let me just change this to the short closure syntax.

Build Forgot Password View0:35

So let's go to that, let's put that here, and let me just change this to the short closure syntax. Okay, and now we need to define that view and it's named forgotPassword. So let's add that in our auth folder here. So let me just duplicate one of these, say forgot_password.blade.php. And let's change this to forgot_password. And all we need here is an email field. And the endpoints are forgotPassword. So let's take a look at our routes, php artisan route:list. And let's look for forgotPassword, or forgot is fine.

So let's take a look at our routes, php artisan route:list. And let's look for forgot password, or forgot is fine. And there are two here. And these do have route names. And usually I make use of route names. But since I haven't been doing that, because some routes don't have route names, then I'll just stick to the endpoint here. So the endpoint is forgot password. So let's add that here. And like I said, all we need is the email field.

So let's add that here. And like I said, all we need is the email field. And what's the button name? What is there in Jetstream? Email password reset link. So we don't need these. And let's just say, email password reset link, okay. And let's also add the confirmation message. So if we scroll down here, there is a check for the success message. So let's copy that and put it below the errors right here.

Add Forgot Password Link2:15

So if we scroll down here, there is a check for the success message. So let's copy that and put it below the errors right here. And that should be good if I did everything correctly. Oh yeah, let's add a link to the login page. So let's go to login.blade.php. And let's just add it under here. Under the form is fine. Let's make an anchor link. Say forgot password. And say forgot password here.

Test Reset Link Email3:14

And now let's add one that is in here. I believe there is one for this. And there is not. Let's try User. I'm not sure if there's a User in here right now. Okay, so there's no users. Let me just add one. Let me try this one. I believe this is the one I have in my database. And yeah, that's the correct one.

Create Reset Password Form3:32

I believe this is the one I have in my database. And yeah, that's the correct one. You see the new email message here. So that's working. And now we have to work on the form that actually resets the password. So that's the next step. If we scroll down, resetting the password, we have to define that view. It's called resetPassword. Let's add this to our service provider. And again, I'm going to change this to the short closure syntax.

Let's add this to our ServiceProvider. And again, I'm going to change this to the short closure syntax. And now we have to add that form. So let's just duplicate the one we just created. Let's call it reset-password.blade.php. And it needs an email. And I'm going to autofocus this. I should autofocus the other one too. So let's just grab that and autofocus the forgot-password. And back to reset.

So let's just grab that and autofocus the forgotPassword. And back to reset. So we also want to populate this field with the actual email address of the User. And that's coming in the request. So we can do $request->email. And we have to change the endpoint. The endpoint is resetPassword. So if I do another search here for reset, we do get those two endpoints there. So let's change that. Let's say resetPassword.

So let's change that. Let's say resetPassword. And we also need fields for the two passwords. So let's just grab that from our register form. This should be fine. And let's go back here. Paste that in. Let's change the button name to resetPassword. And let's paste in those password fields. Okay.

Add Token and Verify5:52

Let's make sure the error messages work. So a non matching password first. Okay. And now let's try a correct password. And I believe it should log us in as well. Oh, yeah, we're missing the token field. So we have to add that as well. So the token field is in the URL. But it also has to be a hidden field on the form. And this just verifies that this is a valid request to reset a password.

But it also has to be a hidden field on the form. And this just verifies that this is a valid request to reset a password. And you can see what I'm talking about here says we need a hidden field named token that contains the value of $request->route('token'). So let's add that to our form. This can go anywhere. I'll put it first. So <input type="hidden" name="token" value="{{ $token }}">.

Name has to be token. And the value is the token. So request route token. Okay. Let's see if this works now. Back to here. Let's try a valid password. And that looked like it worked. And let's go ahead and try to logIn here. And it did work.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟