Tricks

Password confirmation action

May 30, 2022
Jeff Greco
Admin panel, Table builder

Intro: This trick will show you how to ask the user to confirm their password before completing an action.

We will extend the Action class for Pages in the admin panel, but you could instead extend the table builder Action class if you wanted.

First, create a new class called PasswordAction in your Actions folder (ex. app/Filament/Actions):

<?php
 
namespace App\Filament\Actions;
 
use Filament\Forms;
use Filament\Pages\Actions\Action;
 
class PasswordAction extends Action
{
protected function isPasswordSessionValid()
{
return (session()->has('auth.password_confirmed_at') && (time() - session('auth.password_confirmed_at', 0)) < 300); // We won't ask the user for their password again for 300s = 5mins
}
 
protected function setUp(): void
{
parent::setUp();
 
if ($this->isPasswordSessionValid()) {
// Password confirmation is still valid
//
} else {
$this->requiresConfirmation()
->modalHeading("Confirm password")
->modalSubheading(
"Please confirm your password to complete this action."
)
->form([
Forms\Components\TextInput::make("current_password")
->required()
->password()
->rule("current_password"),
]);
}
}
 
public function call(array $data = [])
{
// If the session already has a cookie and it's still valid, we don't want to reset the time on it.
if ($this->isPasswordSessionValid()) {
} else {
session(['auth.password_confirmed_at' => time()]);
}
 
parent::call($data);
}
}

Now, you can use your new class anywhere that Action is supported:

<?php
 
use App\Filament\PasswordAction;
 
PasswordAction::make('secure_action')->action('enableTwoFactor')->icon('heroicon-s-shield-check');

No comments yet…