ddtech/laravel-password-policy

Associate password policy with Eloquent models

dev-main 2021-10-20 20:20 UTC

This package is auto-updated.

Last update: 2024-10-29 06:00:24 UTC


README

This package ensures that the last used (n) password/s are not entered.

Requirements

Laravel Fortify or Jetstream has to be installed.

Installation

composer require ddtech/laravel-password-policy
php artisan vendor:publish --tag=migrations
php artisan migrate

Usage

Add PasswordRecords Traits to User Model

use PasswordRecords;

You need to change Fortify, ResetUserPassword's reset function

Validator::make($input, [
  'password' => $this->passwordRules(),
])->validate();
Validator::make($input, [
    'password' => $this->passwordRules(),
])->after(function ($validator) use ($user, $input) {
    $user->oldPasswordRules($validator, $user, $input, 3);
})->validate();

$user->triggerPasswordRecord($user->password);

You need to change Fortify, UpdateUserPassword's update function

Validator::make($input, [
    'current_password' => ['required', 'string'],
    'password' => $this->passwordRules(),
])->after(function ($validator) use ($user, $input) {
    if (! isset($input['current_password']) || ! Hash::check($input['current_password'], $user->password)) {
        $validator->errors()->add('current_password', __('The provided password does not match your current password.'));
    }
})->validateWithBag('updatePassword');
Validator::make($input, [
    'current_password' => ['required', 'string'],
    'password' => $this->passwordRules(),
])->after(function ($validator) use ($user, $input) {
    if (! isset($input['current_password']) || ! Hash::check($input['current_password'], $user->password)) {
        $validator->errors()->add('current_password', __('The provided password does not match your current password.'));
    }
    $user->oldPasswordRules($validator, $user, $input, 3);
})->validateWithBag('updatePassword');

$user->triggerPasswordRecord($user->password);