Filament Renew Password

Filament Renew Password

Documentation for the Filament Renew Password plugin: force a password reset on first login, add recurring renewal, or both, with the config options and code needed to wire it into your own User model.

Filament v3 Filament v4 Filament v5

Introduction

The Filament Renew Password plugin prompts users to renew their password based on criteria you define. Two renewal processes are built in:

  • Force renewal, useful when an administrator creates a user and sends a temporary password: the user is required to set their own password on first login.
  • Recurring renewal, prompting a password change after a set number of days.

Both can be used independently or together, and you're free to add your own criteria beyond these two.

For the reasoning behind when forced or recurring renewal actually makes sense, and when it doesn't, see Why forced password changes are still everywhere, and why that's changing. This plugin actually started from a much older, simpler version of the same problem: Enforcing password updates for admin-created accounts.

Installation

composer require yebor974/filament-renew-password

Filament v4 and v5 are supported from version 3.x of the plugin. If you're still on Filament v3, install version 2.x instead:

composer require yebor974/filament-renew-password:^2.0

Publish and run the migration, which adds last_renew_password_at and force_renew_password columns to the users table:

php artisan vendor:publish --tag="filament-renew-password-migrations"
php artisan migrate

If you'd rather not publish the migration, or already have equivalent columns, skip this step and customize the column names instead (see Configuration below).

Translations can be published separately:

php artisan vendor:publish --tag="filament-renew-password-translations"

Register the plugin in your panel provider:

use Yebor974\Filament\RenewPassword\RenewPasswordPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->plugin(new RenewPasswordPlugin());
}

Configuration

The plugin works out of the box with minimal setup, but every part of it can be customized: through the config file, environment variables, or directly on the plugin object.

Force renewal

Disabled by default. Useful when an administrator creates a user and sends a temporary password, forcing them to set their own on first login:

RenewPasswordPlugin::make()
    ->forceRenewPassword()

This manages a boolean force_renew_password column automatically. To use a different column name:

RenewPasswordPlugin::make()
    ->forceRenewPassword(forceRenewColumn: 'your_custom_boolean_force_column')

Recurring renewal

Also disabled by default. Activate it by defining how many days a password stays valid:

RenewPasswordPlugin::make()
    ->passwordExpiresIn(days: 30)

This manages a last_renew_password_at timestamp column automatically. To use a different column name:

RenewPasswordPlugin::make()
    ->passwordExpiresIn(days: 30)
    ->timestampColumn('your_custom_timestamp_column')

Combining both

Force renewal on first login, with a custom timestamp column but no recurring expiry:

RenewPasswordPlugin::make()
    ->forceRenewPassword()
    ->timestampColumn('your_custom_timestamp_column')

Both processes together:

RenewPasswordPlugin::make()
    ->passwordExpiresIn(days: 30)
    ->forceRenewPassword()

With both columns customized:

RenewPasswordPlugin::make()
    ->passwordExpiresIn(days: 30)
    ->forceRenewPassword(forceRenewColumn: 'your_custom_boolean_force_column')
    ->timestampColumn('your_custom_timestamp_column')

Custom renew page

A simple renewal page is used by default. Replace it with your own:

RenewPasswordPlugin::make()
    ->renewPage(CustomRenewPassword::class)

Usage

Implement RenewPasswordContract on your authentication model (User), and define the renewal criteria in needRenewPassword().

Default trait

The RenewPassword trait handles both processes for you:

class User extends Authenticatable implements RenewPasswordContract
{
    use RenewPassword;
}

Under the hood, it checks whichever process is active:

public function needRenewPassword(): bool
{
    $plugin = RenewPasswordPlugin::get();

    return
        (
            !is_null($plugin->getPasswordExpiresIn())
            && Carbon::parse($this->{$plugin->getTimestampColumn()})->addDays($plugin->getPasswordExpiresIn()) < now()
        ) || (
            $plugin->getForceRenewPassword()
            && $this->{$plugin->getForceRenewColumn()}
        );
}

Custom criteria

Implement needRenewPassword() yourself on the authentication model for anything beyond the two built-in cases. You'll likely want a custom renew page too, in that case.

Migrating from v1 to v2

v2 no longer enables password renewal automatically. You need to explicitly declare which process(es) to use, as shown above. There are no more associated config files or .env variables.

To restore recurring renewal after upgrading:

RenewPasswordPlugin::make()
    ->passwordExpiresIn(days: 30)

To add force renewal, add the boolean column to your authentication model's table:

$table->boolean('force_renew_password')->default(false);

and declare it as shown in the Configuration section above.

Did this guide save you time? Keeping the site online has a cost, and a one-time donation helps cover the server. Completely optional.

Buy me a coffee →