mjelamanov/laravel-auth-password

A laravel authenticated user's password validator.

1.1.0 2020-12-08 16:03 UTC

This package is auto-updated.

Last update: 2024-12-09 00:18:08 UTC


README

Build Status StyleCI Latest Stable Version License

This package allows you to validate an authenticated user's password. For laravel 6.0 and above please use the native password rule.

Table of Contents

Requirements

  • PHP 7.1 or above.
  • Laravel 5.8 or 6.0.

Installation

composer require mjelamanov/laravel-auth-password

Don't forget to add the translation key bellow to your app's lang files.

// in resources/lang/en/validation.php

'auth_password' => 'The :attribute is not valid',

Use rule

Create a rule instance via RuleFactoryInterface.

// In your controller

use Illuminate\Http\Request;
use Mjelamanov\Laravel\AuthPassword\Rule\RuleFactoryInterface;

public function changeUserPassword(Request $request, RuleFactoryInterface $ruleFactory)
{
    $this->validate($request, [
        'current_password' => ['bail', 'required', 'min:6', $ruleFactory->createRule()],
        'new_password' => 'bail|required|string|min:6',
        'new_password_confirmation' => 'bail|required|confirmed',
    ]);

    // Passwords are valid. Place your logic here.
}

You can specify a different guard name.

$ruleFactory->createRule(); // default application's guard
$ruleFactory->createRule('web');
$ruleFactory->createRule('api');

// or custom guard
$ruleFactory->createRule('admin');

$ruleFactory->createRule('non-existen'); // Throws \InvalidArgumentException 

Use extension

You may use the validation extension also.

// In your controller

public function changeUserPassword(Request $request)
{
    $this->validate($request, [
        'current_password' => 'bail|required|min:6|auth_password',
        'new_password' => 'bail|required|string|min:6',
        'new_password_confirmation' => 'bail|required|confirmed',
    ]);

    // Passwords are valid. Place your logic here.
}

You can also specify a different guard name.

'field' => 'auth_password', // default application's guard
'field' => 'auth_password:web',
'field' => 'auth_password:api',

// or custom guard
'field' => 'auth_password:admin',

'field' => 'auth_password:non_existen', // Throws \InvalidArgumentException