Example of Laravel password retrieval and password reset

Source: Internet
Author: User
Tags auth button type

Forgetting a password is one of the common scenarios in applications. Laravel 5.1 also provides support for password resetting. You only need to make a slight configuration to easily reset the password.

1. Implementation ideas

By sending a password reset link containing a specific token to the user's registered mailbox, the user can log on to the mailbox to reset the password by accessing the password reset link.

2. Data tables and models

Implement the CanResetPasswordContract contract and use the CanResetPasswordtrait User model (included in Laravel)
The password_resets table used to store the password reset token (Laravel comes with the corresponding migration file for this table, which has been created in the previous section)

3. Create a route

Laravel comes with the Auth \ PasswordController for password reset, which is located in the same directory as the AuthController mentioned in the previous section. The business logic related to password resetting is implemented through the ResetsPasswordstrait used in the controller. Next we will define relevant routing rules for password resetting in routes. php:

// Send a password to reset the link route
Route: get ('password/email ', 'auth \ PasswordController @ getEmail ');
Route: post ('password/email ', 'auth \ PasswordController @ postemail ');

// Reset the route using the password
Route: get ('password/reset/{token} ', 'auth \ PasswordController @ getreset ');
Route: post ('password/reset', 'auth \ PasswordController @ postreset ');

4. Create a view

After the route is defined, we define the corresponding view file for the get request. First, create the View resources/views/auth/password. blade. php for the send password reset link route:

<Form method = "POST" action = "/password/email">
{!! Csrf_field ()!!}

<Input type = "email" name = "email" value = "{old ('email ')}">

<Button type = "submit">
Send password reset link
Then create the View resources/views/auth/reset. blade. php for resetting the password route:

<Form method = "POST" action = "/password/reset">
{!! Csrf_field ()!!}
<Input type = "hidden" name = "token" value = "{{$ token}">

Email: <input type = "email" name = "email" value = "{old ('email ')}">

New password: <input type = "password" name = "password">

Confirm password: <input type = "password" name = "password_confirmation">

<Button type = "submit">
Reset password
In addition, we also need to create an additional view-mail template view for sending password reset links resources/views/emails/password. blade. php, which is used to provide a view template for this mail:

Click here to reset the password: {url ('password/reset/'. $ token )}}
If the file path of the mail template view is elsewhere, do not forget to configure the password. email value in config/auth. php to correspond to the new path.

5. Mail sending configuration

Next, we need to configure relevant files to implement the Mail sending function to prepare for the next Test.

Laravel uses the mail API provided by the SwiftMailer library to perform mail operations. For more information, see The mail Documentation. Here we only configure mail sending. The mail configuration file is config/mail. php:

<? Php

Return [
'Driver '=> env ('mail _ driver', 'smtp '),
'Host' => env ('mail _ host', 'smtp .mailgun.org '),
'Port' => env ('mail _ port', 587 ),
'From' => ['address' => null, 'name' => null],
'Encryption' => env ('mail _ encryption', 'tls '),
'Username' => env ('mail _ username '),
'Password' => env ('mail _ password '),
'Sendmail' => '/usr/sbin/sendmail-bs ',
'Pretend' => false,

Most of the configurations are set in the. env file. The configuration of my. env file is as follows:

MAIL_HOST = smtp.163.com
MAIL_USERNAME = yaojinbu@163.com
MAIL_PASSWORD = mypassword

I use mailbox 163. For other mailboxes, refer to the corresponding mailbox settings and enter your account information in MAIL_USERNAME and MAIL_PASSWORD.

In addition, we also need to configure the from configuration in mail. php as follows:

'From' => ['address' => 'aojinbu @ 163.com ', 'name' => 'laravel '],
Here, you only need to match the MAIL_USERNAME value in the address and. env files. As for the name value, it is the sender name in the mailbox, which can be customized.

After completing this step, you can test the password reset.

6. Reset the password

Access http://laravel.app in a browser: 8000/password/email, the page is shown as follows:

Laravel sends a password reset email

In the Email input box, enter your registered Email address, click "send password reset link", and check the inbox in the Email address. If the Email is sent successfully, you can receive the following password reset Email:

Click here to reset the password:

Ttp: // laravel. app: 8000/password/reset
In this case, a record is added to the password_resets table to save the reset link token:

Laravel saves and resets the password token

By default, this token is saved for one hour. To maintain the validity period, you can edit password. expire in config/auth. php.

Copy the link in the password reset email and paste it to the address bar of the browser and press Enter. The page displays the following content:

Laravel password reset page

Enter this form and click Reset password to complete password reset.

After the password is reset, the default redirect link is/home. In PasswordController, you can set the value of $ redirectTo/$ redirectPath to modify the redirect link:

Protected $ redirectPath = '/profile ';

The http://laravel.app: 8000/profile:

Test logon successful!

At the same time, the corresponding records in password_resets will also be deleted.

Now, you can use the new password for logon authentication next time.

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.