Table of contents

laravel strong password

In this post, you will learn how to implement Laravel's strong password in the validation rule. Starting with Laravel 8.x they provide an easier implementation for a strong password that doesn't need extra coding.

 

Usually, when we create a simple login we just validate the minimum length of the user password submitted but if we want a more secured user account we force them to provide a password that is difficult to hint.

 

laravel strong password validation

 

 

Password with minimum length.

As you can see below we added Password static class with min(8) method. This means that the password should have at least 8 minimum characters.

 

<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rules\Password;
use Illuminate\Foundation\Http\FormRequest;

class RegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'password' => [
                'required',
                Password::min(8)
            ],
            'password_confirmation' => 'required|same:password'
        ];
    }
}

 

Password contain at least 1 letter

The next example is a password of at least 1 letter.

public function rules()
{
    return [
         'password' => [
             'required',
             Password::min(8)
               ->letters()
         ],
         'password_confirmation' => 'required|same:password'
    ];
}

 

The password contains at least 1 lowercase and 1 uppercase

In this example a password at least 1 lowercase and 1 uppercase.

public function rules()
{
    return [
         'password' => [
             'required',
             Password::min(8)
               ->mixedCase()
         ],
         'password_confirmation' => 'required|same:password'
    ];
}

 

The password contains at least 1 number

In this example, a password is at least 1 number.

public function rules()
{
    return [
         'password' => [
             'required',
             Password::min(8)
               ->numbers()
         ],
         'password_confirmation' => 'required|same:password'
    ];
}

 

The password contains at least 1 symbol

In this example, a password is at least 1 symbol.

public function rules()
{
    return [
         'password' => [
             'required',
             Password::min(8)
               ->symbols()
         ],
         'password_confirmation' => 'required|same:password'
    ];
}

 

In addition, if you want to ensure that the password submitted is not compromised on the internet with a public password data breach leak use the following lines below:

 

Password::min(8)->uncompromised();

 

You can also add a value if you want to ensure that the password appears less than 3 times in the same data leak.

 

Password::min(8)->uncompromised(3);

 

Here is the complete validation with the chaining method for password validation.

 

<?php

namespace App\Http\Requests;

use Illuminate\Validation\Rules\Password;
use Illuminate\Foundation\Http\FormRequest;

class RegisterRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'email' => 'required|email:rfc,dns|unique:users,email',
            'username' => 'required|unique:users,username',
            'password' => [
                'required',
                Password::min(8)
                    ->letters()
                    ->mixedCase()
                    ->numbers()
                    ->symbols()
                    ->uncompromised()
            ],
            'password_confirmation' => 'required|same:password'
        ];
    }
}

 

I hope it helps now you know already how to implement the Laravel strong password. Thank you for reading.