Detailed examples Laravel password reset code reconstruction, examples laravel

Source: Internet
Author: User

Detailed examples Laravel password reset code reconstruction, examples laravel

1. First, confirm the route for resetting the password.

When laravel is installed, the Reset Password generated by default is performed without logon. Therefore, using the original controller is not feasible, and the original password reset does not need to check whether the original password is correct, but directly change the password through email, we also need to write a new one. We usephp artisan make:controller UserControllerCreate a controller class and then create two routesRoute::get('reset', 'UserController@getReset')AndRoute::post('reset', 'UserController@postReset').

The former shows a page get request for password reset, followed by a password reset post request.

2. The Reset Password page is displayed.

This isgetResetThis method only needs to display a view, so there is no special logic

public function getReset(){  return view('auth.reset');}

3. Request Password Reset

This ispostResetIn this method, we can use either of the following methods to receive data:One is to use the request Method to receive data.,Another method is to use Input: get to obtain data..RequestYou need to introduceuse Illuminate\Http\RequestClass,InputYou need to introduceuse InputClass, here we choose to userequest.

4. verification rules

For verification, laravel provides us with a set of verification rules, usingvalidatorOfValidator::make()Method Verification

$ Data = $ request-> all (); // receives all data $ rules = ['oldpassword' => 'required | between: 6, 20 ', 'Password' => 'required | between: 6, 20 | confirmed ',]; $ messages = ['requestred' => 'password cannot be blank ', 'between' => 'the password must be 6 ~ Between 20 bits, 'confirmed '=> 'new password does not match confirm password']; $ validator = Validator: make ($ data, $ rules, $ messages );

$data Received data information from;

rulesJudge the received value.oldpasswordAndpasswordIs to verify the data in the name field of the original password and new password received from the front end;

The verification rules are included in the Verification Section of the manual. It is worth noting that confirmed is used to make the same judgment for the new password and the confirmed password, confirm that the password must have a name value followed by the name value of the new password'_confirmation'For example, the name of the new password isnewpasswordThe password name must benewpassword_confirmationCan be judgedmessagesWhat prompt is displayed for the data request for verification.

After the above verification, there is another situation that is not verified, that is, whether the entered original password is the same as the original password in the database.

Here, we can first find out the user information from the database, and then compare it with the entered original password. Here we useAuth::user()To obtain user information, this method needs to be introduceduse Auth;Class, and thenHash::check()To determine the password. There is another problem after the judgment, that is, how to push the error information to the error message of validator. laravel provides the after method for us:

$ User = Auth: user (); $ validator-> after (function ($ validator) use ($ oldpassword, $ user) {if (! \ Hash: check ($ oldpassword, $ user-> password) {// compare the original password with the password in the database $ validator-> errors () -> add ('oldpassword', 'original password error'); // The original password is incorrectly displayed.}); if ($ validator-> fails ()) {// determine whether an error is returned back ()-> withErrors ($ validator); // redirect the page, and save the error information to the one-time session} $ user-> password = bcrypt ($ password); // use the bcrypt function to encrypt the new password $ user-> save (); // Save the new password

This is becauseafter A PHP anonymous function is introduced, so we need to useuse Keywords: pass external data to anonymous functions (PS: New Features of php, closures and anonymous functions)

We have introduced a global function in the anonymous function, so we need to add \ (PS: New php features, namespace chapter, global namespace) before the function)

5. Front-end error messages

For front-end display, we use$errorsVariable to display the error. According to the official documentation, the call isIlluminate\Support\MessageBagFor more information, see. We usecount($errors) > 0To determine whether there is an error. $errors->first()An error message is displayed:

@if(count($errors) > 0)  <div class="alert alert-danger display-hide" style="display: block;">    <button class="close" data-close="alert"></button>    <span>  </span>  </div>@endif

Someone may ask, if my error is not displayed in a fixed place, but an error message is displayed behind each form, how can we judge and display it? The answer is to use$errors->has('oldpassword')To determine whether this name is correct. If yes, use$errors->first('oldpassword') This error is displayed:

@if( $errors->has('oldpassword') )  <div class="alert alert-danger display-hide" style="display: block;">    <button class="close" data-close="alert"></button>    <span>  </span>  </div>@endif

WhereoldpasswordIs the name value in each form, soafterWhen a custom method is added$ Validator-> errors ()-> add ('oldpassword', 'incorrect original password ');Medium,oldpasswordMake sure that the correct form is incorrect so that it can be correctly displayed.

6. example after completion

UserController

Public function getReset () {return view ('auth. reset');} public function postReset (Request $ request) {$ oldpassword = $ request-> input ('oldpassword '); $ password = $ request-> input ('Password'); $ data = $ request-> all (); $ rules = ['oldpassword' => 'required |: 6, 20 ', 'Password' => 'required | between: 6, 20 | confirmed',]; $ messages = ['requestred' => 'password cannot be blank ', 'between' => 'the password must be 6 ~ Between 20 bits, 'confirmed '=> 'new password does not match confirm password']; $ validator = Validator: make ($ data, $ rules, $ messages ); $ user = Auth: user (); $ validator-> after (function ($ validator) use ($ oldpassword, $ user) {if (! \ Hash: check ($ oldpassword, $ user-> password) {$ validator-> errors ()-> add ('oldpassword ', 'original password error') ;}}); if ($ validator-> fails () {return back ()-> withErrors ($ validator ); // return a one-time error} $ user-> password = bcrypt ($ password); $ user-> save (); Auth: logout (); // after changing the password, exit the user return redirect ('/login ');}

Reset. blade

<Form class = "login-form" action = "" method = "post"> 

Summary

The above is all the content in this article. I hope it will help you learn how to use Laravel. If you have any questions, please leave a message to discuss it.

Original article: Dennis's blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.