π Passwords made easy in Filament PHP π€«
This package allows you to add a feature rich password input/field for Filament PHP.
https://packagist.org/packages/discoverydesign/filament-locksmith
composer require discoverydesign/filament-locksmith
- Ability to copy password
- Ability to automatically generate passwords. Default to 32 random character string.
- User-friendly preset generator, creates 3 word combo passwords. E.g:
elephant-plant-photo
. - Automatically hash password when storing to database. Useful when a cast can't be used.
- Block password field being editable, forcing a randomly generated password.
- Install the package using
composer require discoverydesign/filament-locksmith
- Import the package inside your Filament Form with
use DiscoveryDesign\FilamentLocksmith\Forms\Components\PasswordInput
. - Add the
PasswordInput
form component to your form withPasswordInput::make()
. - If required, publish the translation files with
php artisan vendor:publish --tag=filament-locksmith-translations
.
<?php
namespace App\Filament\Resources;
use DiscoveryDesign\FilamentLocksmith\Forms\Components\PasswordInput;
// ...
class UserResource extends Resource
{
// ...
public static function form(Form $form): Form
{
return $form
->schema([
PasswordInput::make('password')
->required()
->generatable()
->friendly()
->copyable()
->revealable(),
// ...
]);
}
// ...
}
PasswordInput::make('password')
->required()
->advanced()
->copyable()
->revealable(),
PasswordInput::make('password')
->required()
->advanced()
->setGenerators([
new DiscoveryDesign\FilamentLocksmith\Generators\RandomGenerator,
new DiscoveryDesign\FilamentLocksmith\Generators\MemorableGenerator
])
->copyable()
->revealable(),
generatable
can be used to set if this password field should allow for an automatic password to be generated. This by default will generate a 32 character random string.
state
- (optional, bool) If the password should be generatable.
friendly
is a preloaded generator that creates a user friendly password. This password consists of 3 words that are combinred with '-'. E.g time-shelf-bottle
copyable
can be used to set if this password field should copyable to clipboard.
state
- (optional, bool) If the password should be copyable.
editable
can be used to block the password field being edited. This is normally combined with ->generatable()
.
state
- (optional, bool) If the password should be editable.
generator
allows you to define a custom generator that is used to create a password.
func
- (optional, closure | bool) The function used to generate the password. This function must return a string.
hashed
can be used if the password should be hashed before being stored. In most cases, you should instead use a cast against your model.
state
- (optional, bool) If the password should be hashed.
revealable
can be used to allow the password to be revealed. This is just Filament's built in reveal functionality.
func
- (optional, closure | bool) If the password should be revealable. If a closure is passed, this should return a bool.
advanced
will enable advanced mode which allows user configuration of their password, along with a selection of different password types.
state
- (optional, bool) If the password should be hashed.
addGenerator
can be used to add a generator to the advance mode password generator. You should pass in an instance of a class that extends DiscoveryDesign\FilamentLocksmith\Generators\BaseGenerator
.
generator
- (class, extends BaseGenerator) The generator to add.
setGenerators
will override all existing generators assigned to this PasswordInput and instead use the ones passed in. You should pass in an array of instances of a class that extends DiscoveryDesign\FilamentLocksmith\Generators\BaseGenerator
.
generators
- (array) The generators to set.
getGenerators
can be used to get an array of all the current generators.
If you want to create a generator, you should first start by creating a new class that extends DiscoveryDesign\FilamentLocksmith\Generators\BaseGenerator
.
Inside your __construct
you should include any options that you want to use to generate the password. It is encouraged to use a unique name for your form inputs.
The generate
function should return a string that is the password.
<?php
namespace App\Filament\Locksmith\Generators;
use Filament\Forms;
use DiscoveryDesign\FilamentLocksmith\Generators\BaseGenerator;
use Illuminate\Support\Str;
class MyCustomGenerator extends BaseGenerator
{
public string $name = 'My Custom Generator';
public function __construct()
{
$this->setOptions([
Forms\Components\TextInput::make('mygenerator_length')
->label('length')
->default(20)
->type('number')
->required()
]);
}
public function generate($get)
{
$length = $get('mygenerator_length');
return Str::password($length);
}
}
You can then add this generator to your password input with ->addGenerator(new MyCustomGenerator)
.
π Discovery Design