Skip to content

Latest commit

 

History

History
128 lines (95 loc) · 3.74 KB

Passwords.md

File metadata and controls

128 lines (95 loc) · 3.74 KB

Overview

This feature provides a trait that acts as a wrapper for both Laravel's ResetsPasswords and SendsPasswordResetEmails traits.

It's role is to give more flexibility in setting properties that are hard-coded in the framework.

Please check Laravel Password Reset Documentation for more information about the password resetting and recovering process.

Usage

The CanResetPassword Trait

Your reset/forgot password controllers should use the App\Traits\CanResetPassword trait and implement the getResetPasswordOptions() method.

The CanResetPassword trait uses as its ground-base the original Laravel's ResetsPasswords and SendsPasswordResetEmails traits.

// in your ForgotPasswordController.php file

use App\Http\Controllers\Controller;
use App\Traits\CanResetPassword;
use App\Options\ResetPasswordOptions;

class ForgotPasswordController extends Controller
{
    use CanResetPassword;

    /**
     * @return ResetPasswordOptions
     */
    public static function getResetPasswordOptions()
    {
        return ResetPasswordOptions::instance()
            ->setValidator(new ResetPasswordRequest);
    }
}

// in your ResetPasswordController.php file

use App\Http\Controllers\Controller;
use App\Traits\CanResetPassword;
use App\Options\ResetPasswordOptions;

class ResetPasswordController extends Controller
{
    use CanResetPassword;

    /**
     * @return ResetPasswordOptions
     */
    public static function getResetPasswordOptions()
    {
        return ResetPasswordOptions::instance()
            ->setValidator(new ResetPasswordRequest);
    }
}

Please note that defining the getResetPasswordOptions() method is mandatory.
If you fail in doing so, the code will throw an error indicating this to you.

The getResetPasswordOptions() should always return an App\Options\ResetPasswordOptions instance.

You can view all options and their methods of implementation inside the App/Options/ResetPasswordOptions.php file.

However, please remember that you are required to specify a validator form request by which the password reset/recover functionalities will apply its validation rules.

You can do this using the setValidator() method.

If you fail in implementing this method, the code will throw an error indicating this to you.

The ResetPasswordOptions Class

With the help of the getResetPasswordOptions() method defined on your given controllers, you can customize the trait's behavior.

Specify a guard for the password reset/recover functionalities.

/**
 * @return ResetPasswordOptions
 */
public static function getResetPasswordOptions()
{
    return ResetPasswordOptions::instance()
        ->setValidator(new ResetPasswordRequest)
        ->setAuthGuard('admin');
}

Set a different identifier field.

/**
 * @return ResetPasswordOptions
 */
public static function getResetPasswordOptions()
{
    return ResetPasswordOptions::instance()
        ->setValidator(new ResetPasswordRequest)
        ->setIdentifier('username');
}

Set the path to redirect after password reset/recover.

/**
 * @return ResetPasswordOptions
 */
public static function getResetPasswordOptions()
{
    return ResetPasswordOptions::instance()
        ->setValidator(new ResetPasswordRequest)
        ->setRedirect(route('login'));
}

How To

There is nothing custom to know here.

The only thing you need to know is how the Laravel's Password Reset Process works.