How to strengthen password validations in Laravel

Laravel 8 provides an object that allows you to modify the complexity of the passwords in your applications. You can also ensure that the password has not been compromised in data leaks.

This object has the following methods:

$request->validate([
    
    'password' =>  ['required', 'confirmed', Password::min(8)->mixedCase()],

    
    'password' =>  ['required', 'confirmed', Password::min(8)->letters()],

    
    'password' =>  ['required', 'confirmed', Password::min(8)->numbers()],

    
    'password' =>  ['required', 'confirmed', Password::min(8)->symbols()],

    
    'password' =>  ['required', 'confirmed', Password::min(8)->uncompromised()],
]);

Methods

We use five methods to strengthen password validation:

  1. mixedCase() – ensures and validates that the user enters a combination of both uppercase and lowercase letters

  2. letters() – validates that the user enters a letter

  3. numbers() – validates that the user’s password contains a number

  4. symbols() – ensures that the password contains a symbol

  5. uncompromised() – validates the password to ensure that it hasn’t appeared in a public data leak.

To implement password validation, we use the following methods (as follows) in the validations that are located in the controllers:

public function store(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => ['required', 'confirmed', Password::min(8)
        ->mixedCase()
        ->letters()
        ->numbers()
        ->symbols()
        ->uncompromised(),
            ],
        ]);

The code above won’t consider user entries to be validated unless the password has mixed-case, letters, numbers, symbols, and is uncompromised. The code incorporates all five methods to ensure the validation is strengthened.

Free Resources