Recovery Password

Setting

This procedure uses the forgot method to define the database table. The columns method the form's email field. In the values method, the e-mail that will be sent the recovery link is informed in the first parameter, and in the second parameter the URL that will be contained in the e-mail to change the password. The register method will check and send the email.

/**
 * @return void
 */
public function forgotPost(): void
{
    $email = input()->post('email')->getValue();

    $res = Auth::forgot('auth_users')
            ->columns('username')
            ->values($email, url('change'))
            ->register();

    if ($res == true) {
        $this->message->new('forgot', 'Link sent to your email!');
        response()->redirect(url('forgot'));
    }
}

Setting expiration time

By default, the link sent is valid for 1 hour. You can change this behavior using the timeHash() method.

Auth::forgot('auth_users')
    ->columns('username')
    ->values($email, url('change'))
    # Here the code
    ->timeHash('+2 hours')
    ->register();

Changing email fields

By default, "User" is sent as the name of the sender and recipient. "Forgot Password" as the title of the email.

To change these fields, use the fields() function.

Auth::forgot('auth_users')
    ->columns('username')
    ->values($email, url('change'))
    # Here the code
    ->fields('name_sender', 'name_recipient', 'subject')
    ->register();

Changing default email

If you need to change the default password recovery email, you must first use the generateLink() function. This function generates a new link in which the user will be redirected when changing the password.

First, it is necessary to inform the user's email, the route he will access to change the password, and the length of time that this link will be valid.

The code below shows an example of this use:

$msg = "<h1>Retrieve your password</h1>";
$msg .= "<p>Click the link below to change your password</p>";
$msg .= "<a href='".generateLink($email, url('change'), '+2 hours')."'>Change Here!!</a>";

Auth::forgot('auth_users')
    ->columns('username')
    ->values($email, url('change'))
    # Here the code
    ->fields('name_sender', 'name_recipient', 'subject', $msg)
    ->register();

To validate the information by clicking on the email link, you can use the structure below:

/**
 * @param string $hash
 * 
 * @return void
 */
public function change($hash): void
{
    $res = Hash::decrypt($hash)->isValid();

    if ($res == true) {
        $email = Hash::decrypt($hash)->value();

        return view('auth.change-pass-form', [
            'title' => 'Change Password',
            'email' => $email,
            'hash' => $hash
        ]);
    } else {
        $this->message->new('login', 'The informed link has already expired!');
        response()->redirect(url('auth'));
    }
}

Changing the password

This procedure uses the change method to define the database table. The columns method defines the database user and password fields. The values method defines the user's email in the first parameter, and the new password in the second parameter. The register method will check and change the email.

Auth::change('auth_users')
    ->columns('username', 'password')
    ->values($email, $pass)
    ->register();

Password recovery structure

You can create a predefined password recovery framework. To do so, use the php vinci auth:skeleton --forgot command.

This command creates a controller with the name ForgotController. With it you will have all the basis to create a password recovery system.

If you want to remove this structure, use php vinci auth:skeleton --forgot --remove.


What to see next?


Built with MkDocs.