Prevent old password as new password

I need help to tp prevent user entering the same old password as new password
Solution:
or ```php TextInput::make('password')->rules([ fn (): Closure => function (string $attribute, $value, Closure $fail) {...
Jump to solution
5 Replies
toeknee
toeknee3mo ago
use a custom rule?
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Hash;

class NotSamePassword implements ValidationRule
{
protected $user;

public function __construct($user)
{
$this->user = $user;
}

public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (Hash::check($value, $this->user->password)) {
$fail('The new password must be different from your current password.');
}
}
}
<?php

namespace App\Rules;

use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Facades\Hash;

class NotSamePassword implements ValidationRule
{
protected $user;

public function __construct($user)
{
$this->user = $user;
}

public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (Hash::check($value, $this->user->password)) {
$fail('The new password must be different from your current password.');
}
}
}
Adnan Yalahow
Adnan Yalahow3mo ago
I am using custom edit profile so how do i use it
<?php

namespace App\Filament\Pages\Auth;

use Filament\Forms\Components\Component;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Auth\EditProfile as BaseEditProfile;
use Filament\Pages\Dashboard;

class EditProfile extends BaseEditProfile
{
public function form(Form $form): Form{
return $form
->schema([
$this->getNameFormComponent(),
$this->getPhoneComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent()
]);
}

protected function getEmailFormComponent(): Component
{
return TextInput::make('email')
->label(__('filament-panels::pages/auth/edit-profile.form.email.label'))
->disabled()
->required()
->maxLength(255)
->unique(ignoreRecord: true);
}

protected function getPhoneComponent(): Component
{
return TextInput::make('phone')
->label('Phone')->disabled();
}

protected function getRedirectUrl(): ?string
{
return Dashboard::getUrl();
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['password_updated'] = true;
return $data;
}
}
<?php

namespace App\Filament\Pages\Auth;

use Filament\Forms\Components\Component;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Pages\Auth\EditProfile as BaseEditProfile;
use Filament\Pages\Dashboard;

class EditProfile extends BaseEditProfile
{
public function form(Form $form): Form{
return $form
->schema([
$this->getNameFormComponent(),
$this->getPhoneComponent(),
$this->getEmailFormComponent(),
$this->getPasswordFormComponent(),
$this->getPasswordConfirmationFormComponent()
]);
}

protected function getEmailFormComponent(): Component
{
return TextInput::make('email')
->label(__('filament-panels::pages/auth/edit-profile.form.email.label'))
->disabled()
->required()
->maxLength(255)
->unique(ignoreRecord: true);
}

protected function getPhoneComponent(): Component
{
return TextInput::make('phone')
->label('Phone')->disabled();
}

protected function getRedirectUrl(): ?string
{
return Dashboard::getUrl();
}
protected function mutateFormDataBeforeSave(array $data): array
{
$data['password_updated'] = true;
return $data;
}
}
toeknee
toeknee3mo ago
see:https://filamentphp.com/docs/3.x/forms/validation Create the above NotSamePassword in App\Rules and then call that rule class in the https://filamentphp.com/docs/3.x/forms/validation#custom-rules ->rules([new NotSamePassword()])
Solution
toeknee
toeknee3mo ago
or
TextInput::make('password')->rules([
fn (): Closure => function (string $attribute, $value, Closure $fail) {
if (Hash::check($value, $this->user->password)) {
$fail('The new password must be different from your current password.');
}
},
])
TextInput::make('password')->rules([
fn (): Closure => function (string $attribute, $value, Closure $fail) {
if (Hash::check($value, $this->user->password)) {
$fail('The new password must be different from your current password.');
}
},
])
Adnan Yalahow
Adnan Yalahow3mo ago
Thank you so much. i want to add that $this->user->password didn't work so i used auth()->user()->getAuthPassword()
Want results from more Discord servers?
Add your server