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

Password Reset Overview0:00

Okay, let's take a look at resetting passwords. So this is two steps. The first step is to send out an email if you forgot your password. And the second step is to actually reset your password. And again, Laravel has built-in functionality for this. So let's go ahead and make use of it. So this video is pretty much just going to be copying and pasting from Breeze's implementation. But I just want to do this so we have a complete example for our manual auth project. So let's go ahead and start with forgot password. I believe there is a link here on the form, and there is.

Add Forgot Password Routes0:25

So let's go ahead and start with forgotPassword. I believe there is a link here on the form, and there is. So let's go into our code. Let's go into the Breeze routes. And let's grab these two forgotPassword ones and let's put it into our application. So routes/web.php for our app. Let's put it here. Let's put it underneath logout. Let's make a new controller called ForgotPasswordController. And we can leave everything else.

Build Forgot Password Form0:55

Let's make a new controller called ForgotPasswordController. And we can leave everything else. So let's go ahead and make that. Say ForgotPasswordController. Okay. And we just need the create and store methods. Actually, let's make the view first. forgot-password.blade.php. And again, I'm just going to paste in a form here. Same thing.

And again, I'm just going to paste in a form here. Same thing. Errors. Here's a form. We just have an email input and a submit button, and the endpoint is forgot password. Okay, let's go ahead and go into that ForgotPasswordController. Let's return our view. return view of forgot password. Okay, let's make sure this goes to that, and it does not. ForgotPasswordController does not exist.

Okay, let's make sure this goes to that, and it does not. ForgotPasswordController does not exist. I need to import it in my routes. So I should import that. Try again. Sorry, this belongs in the create method. Don't need the index method. So let me just get rid of that. Actually, let me get rid of everything else. We don't need all of this.

Actually, let me get rid of everything else. We don't need all of this. Okay. And now this should show the form. There it is. And again, I'm just going to grab what Breeze is doing. So let's look at the PasswordResetLinkController. And we did this already. We want to look at the store method. So some validation here.

Send Reset Link Email2:47

We want to look at the store method. So some validation here. And Laravel has this built in class to handle resetting links or sending resetting links. So again, you can do this manually if you like. You would have to create a unique token. So you know the request is coming from the same user. You'd have to make a Mailable so you can send the email. But like I said, I'm just going to make use of what Laravel already has baked in. So just calling this method. And this returns a status.

So just calling this method. And this returns a status. And if it's successful, then return back with that status. If it's not, then return back with some errors. So let's grab this whole thing and put it into our project in the store method. And this should work. Let me set my mailer to log. So where is it? Mail, mailer, log. And let's see if this works.

And I think that's the only thing I have to import. Let me just do that again. Password reset not defined. So yeah, we have to have that route defined because it needs it when it sends the email. So let's do that. I'm going to go back to Breeze, go back to the auth routes, and let's grab the other one as well. So it's looking for this route right here. Password reset. Right.

Password reset. Right. Yeah. So let's grab these two as well and put it into our app. Back here. Put it underneath here. And let's call ours ResetPasswordController. ResetPasswordController. And we'll work on this after we get the email sending. Let's just make that controller for now.

And we'll work on this after we get the email sending. Let's just make that controller for now. Reset password Controller. Okay. I have an error. No semicolon right here. Try that again. There we go. There we go. And let's see if this works now.

There we go. And let's see if this works now. One more time. And there we go. It says we have emailed your password reset link. Let's check our log to see if the email is in there. So laravel.log should be down here somewhere. And there it is right there. Cool. So let's quickly take a look at the underlying code.

Cool. So let's quickly take a look at the underlying code. Go back to our ForgotPasswordController. Let's take a look at this sendResetLink. So I'm going to go into it with F12 and it's this PasswordBroker. And you can see it's grabbing the User based on whatever the User typed in. And if it's successful, then it creates a token. So it knows that this User created that request to reset the password. And down here, it calls this sendPasswordResetNotification method. And this is what actually sends the email using Laravel notifications.

And down here, it calls this sendPasswordResetNotification method. And this is what actually sends the email using Laravel notifications. So if we go here, you'll see that it's using this notify method, which is basic Laravel notifications. And this is the notification here. If I go into that, actually, it's this one right here. So let's go into that. And this is a Notification class. So you'll see the copy of the email somewhere down here. And there it is.

Implement Reset Password Flow6:30

So you'll see the copy of the email somewhere down here. And there it is. Okay. Let's move on to resetting passwords. So just close these and back to our routes file. Actually, we did that already. We just have to make this controller. Actually, we did do that already. ResetPasswordController. We just need the create and store methods.

ResetPasswordController. We just need the create and store methods. So let me get rid of these. And then let's just grab what is in Breeze. So this one's called NewPasswordController. Let's go into that. And the create method requires the $request. So let's make sure to import that. So it needs this so it can populate the $email when the User clicks on the link in the email that they receive.

So it needs this so it can populate the email when the User clicks on the link in the email that they receive. So let's add that to our project. So create takes in the request, request, request, paste that in. And let's make that in here. reset-password.blade.php. Again, it's going to paste in some code here and errors. Here's the form we're posting to this endpoint. We need this hidden field to store the token to make sure it is a legit request. And as you see, this input field or the email field will be populated with the request email

We need this hidden field to store the token to make sure it is a legit request. And as you see, this input field or the email field will be populated with the request email after the user clicks on it in the email. This is their new password and password confirmation and the submit button. OK. And let's take a look at what's going on in the store method. Again, back to Breeze. Scroll down. Some validation. Again, it's just making use of this built in Password class in Laravel.

Some validation. Again, it's just making use of this built in Password class in Laravel. And the work is being done here in this callback. So let me just grab everything and then we'll take a look at the source as well. Emitting this new password event if you want to hook into that from other parts of your app. So let's grab this. Let's put it into our StoreController. OK. And make sure to import everything we need.

OK. And make sure to import everything we need. So Request, I think, is imported already. Guess not. We need this Password. We need this Hash. We need this Str class. This one. We need this PasswordReset event. OK.

We need this password reset event. OK. And I think that's it. So let's see if this works. Let's go back to our Laravel log and let's grab the URL here for resetting our password. So this one here, OK. Let's just put it in here. ResetPasswordController does not exist. Again, forgot to import that in our routes. I'm still getting used to this, actually, with the new route syntax in Laravel 8.

Again, forgot to import that in our routes. I'm still getting used to this, actually, with the new route syntax in Laravel 8. So it's ResetPasswordController, right? Yes. OK. Try again. There we go. Email is populated. The token should be populated as well, and it should be the correct one because we grabbed it from our email.

Review PasswordBroker Internals10:15

And just to make sure, let's try with that new password. And it does work. Cool. So again, let's take a look at the underlying source into this controller, into this reset method. So if we go into this password, this is just the interface, and you can see all of the constants being defined here. But the actual implementation is the password broker. So let's go into that. And we're looking for the reset method.

So let's go into that. And we're looking for the reset method. So PasswordBroker. There it is. And let's look for the reset method here. There it is. And there you see we have some validation. And the thing we're taking a look at is this. So this is a callback. And the work of updating the password is in here.

So this is a callback. And the work of updating the password is in here. So we just want to make sure that this callback is being called. And you can see that right here. And then it deletes the tokens when it no longer needs them and returns a status. So that's basically it. We've taken a look at making use of Laravel's built-in classes to do some basic authentication manually. Now, personally, for my projects, I would just make use of Jetstream or Breeze. But doing everything manually is just as easy if you wanted to go down that route.

Now, personally, for my projects, I would just make use of Jetstream or Breeze. But doing everything manually is just as easy if you wanted to go down that route.

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