Tricks

Hashing password fields and handling password updates

Jun 10, 2022
Dan Harrin
Form builder, Admin panel

You have a password field:

use Filament\Forms\Components\TextInput;
 
TextInput::make('password')
->password()

And you wanted to hash the password when the form is submitted:

use Filament\Forms\Components\TextInput;
use Illuminate\Support\Facades\Hash;
 
TextInput::make('password')
->password()
->dehydrateStateUsing(fn ($state) => Hash::make($state))

But you don't want to overwrite the existing password if the field is empty:

use Filament\Forms\Components\TextInput;
use Illuminate\Support\Facades\Hash;
 
TextInput::make('password')
->password()
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))

However, you want to require the password to be filled on the Create page of an admin panel resource:

use Filament\Forms\Components\TextInput;
use Filament\Pages\Page;
use Illuminate\Support\Facades\Hash;
 
TextInput::make('password')
->password()
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))
->required(fn (string $context): bool => $context === 'create')
avatar

Nice one :) But there's a small typo, the last line should be:

->required(fn (Page $livewire) => ($livewire instanceof CreateRecord))
๐Ÿ‘ 3
avatar

Thanks, I fixed it :)

avatar

@wakota, Its good, but if the resource is --simple, Page is not valid, so the @dan suggestion is better in this case

avatar

Using v3 the last line should be ->required(fn ($livewire) => ($livewire instanceof CreateRecord))

avatar

Changed my auth table to our employee table/resource. Implemented the above on the password field. Was having trouble with this one. Using the above, the password field was populated when editing a user. Assume this caused the contents to get rehashed? Used the below to leave the password blank when populating the edit form...

Forms\Components\TextInput::make('password')
->password()
->afterStateHydrated(function (Forms\Components\TextInput $component, $state) {
$component->state('');
})
->dehydrateStateUsing(fn ($state) => Hash::make($state))
->dehydrated(fn ($state) => filled($state))
->required(fn (string $context): bool => $context === 'create')

Not sure if this is the correct way to do it but it works fine now.

avatar

Thanks it works

๐Ÿ‘ 1