Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I validate password strength in Fortify


How do I validate password strength in Fortify


To validate password strength in Laravel Fortify, you can follow these steps:

1. Create a custom `PasswordValidationRules` trait:
- Create a new file called `PasswordValidationRules.php` in your `app/Actions/Fortify` directory.
- Define your custom password validation rules in the `passwordRules` method.

php
<?php

namespace App\Actions\Fortify;

use Laravel\Fortify\Rules\Password;

trait PasswordValidationRules
{
    /**
     * Get the validation rules used to validate passwords.
     *
     * @return array
     */
    protected function passwordRules()
    {
        return ['required', 'string', new Password, 'confirmed', 'min:8', 'max:20', 'regex:/[a-z]/', 'regex:/[A-Z]/', 'regex:/[0-9]/', 'regex:/[@$!%*#?&amp;]/'];
    }
}

In this example, we've added the following additional password rules:
- `'min:8'`: Minimum length of 8 characters
- `'max:20'`: Maximum length of 20 characters
- `'regex:/[a-z]/'`: Must contain at least one lowercase letter
- `'regex:/[A-Z]/'`: Must contain at least one uppercase letter
- `'regex:/[0-9]/'`: Must contain at least one digit
- `'regex:/[@$!%*#?&]/'`: Must contain at least one special character

2. Use the custom `PasswordValidationRules` trait:
- Open your `CreateNewUser` class located in `app/Actions/Fortify`.
- Import the `PasswordValidationRules` trait and use it in your class.

php
<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    public function create(array $input)
    {
        Validator::make($input, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => $this->passwordRules(), // Use the custom password rules
            'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['required', 'accepted'] : '',
        ])->validate();

        // ...
    }
}

By following these steps, you've added custom password validation rules to your Fortify-powered registration process. The password must now meet the specified requirements, including a minimum length of 8 characters, a maximum length of 20 characters, at least one lowercase letter, one uppercase letter, one digit, and one special character.

You can further customize the password rules based on your application's specific requirements. Remember to clear the configuration cache after making changes to the password rules:

bash
php artisan config:clear

This ensures that the updated password rules are properly loaded by Fortify.

Citations:
[1] https://wpwebinfotech.com/blog/laravel-fortify/
[2] https://www.youtube.com/watch?v=CLsyHP7x0N0
[3] https://laravel.com/docs/11.x/fortify
[4] https://github.com/laravel/fortify
[5] https://laracasts.com/discuss/channels/laravel/laravel-fortify-post-request-to-reset-password-not-working