Prevent Logout when changing Admin Password
I created a Page called
Also, Everytime I hit submit form, it redirects to
Change Password
, and the form is just this:
<?php
namespace App\Filament\Pages;
use Auth;
use Filament\Pages\Page;
use Filament\Facades\Filament;
use Illuminate\Support\HtmlString;
use Filament\Forms\Components\Grid;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Forms\Components\Placeholder;
use Phpsa\FilamentPasswordReveal\Password;
use App\Notifications\Admin\PasswordUpdated;
use Illuminate\Validation\ValidationException;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
class ChangePassword extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationGroup = 'Settings';
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static string $view = 'filament.pages.change-password';
public $password, $password_confirmation;
protected function getFormSchema() : array
{
return [
Password::make('password')
->label('New Password')
->revealable()
->minLength(8)
->maxLength(16)
->required()
->confirmed(),
Password::make('password_confirmation')
->label('Confirm Password')
->revealable()
->required()
];
}
public function update()
{
$data = $this->form->getState();
$user = Auth::user();
$user->password = $data['password'];
$user->save();
$user->notify(new PasswordUpdated);
return redirect('admin');
}
}
<?php
namespace App\Filament\Pages;
use Auth;
use Filament\Pages\Page;
use Filament\Facades\Filament;
use Illuminate\Support\HtmlString;
use Filament\Forms\Components\Grid;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Forms\Components\Placeholder;
use Phpsa\FilamentPasswordReveal\Password;
use App\Notifications\Admin\PasswordUpdated;
use Illuminate\Validation\ValidationException;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
class ChangePassword extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationGroup = 'Settings';
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static string $view = 'filament.pages.change-password';
public $password, $password_confirmation;
protected function getFormSchema() : array
{
return [
Password::make('password')
->label('New Password')
->revealable()
->minLength(8)
->maxLength(16)
->required()
->confirmed(),
Password::make('password_confirmation')
->label('Confirm Password')
->revealable()
->required()
];
}
public function update()
{
$data = $this->form->getState();
$user = Auth::user();
$user->password = $data['password'];
$user->save();
$user->notify(new PasswordUpdated);
return redirect('admin');
}
}
/login
and not /admin/login
.3 Replies
Up
Because you have invalidated the session by updating the password, you will need to start a new session token after updating the password.
so
<?php
namespace App\Filament\Pages;
use Auth;
use Filament\Pages\Page;
use Filament\Facades\Filament;
use Illuminate\Support\HtmlString;
use Filament\Forms\Components\Grid;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Forms\Components\Placeholder;
use Phpsa\FilamentPasswordReveal\Password;
use App\Notifications\Admin\PasswordUpdated;
use Illuminate\Validation\ValidationException;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
class ChangePassword extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationGroup = 'Settings';
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static string $view = 'filament.pages.change-password';
public $password, $password_confirmation;
protected function getFormSchema() : array
{
return [
Password::make('password')
->label('New Password')
->revealable()
->minLength(8)
->maxLength(16)
->required()
->confirmed(),
Password::make('password_confirmation')
->label('Confirm Password')
->revealable()
->required()
];
}
public function update()
{
$data = $this->form->getState();
$user = Auth::user();
$user->password = $data['password'];
$user->save();
$user->notify(new PasswordUpdated);
Auth::login($user);
return redirect('admin');
}
}
<?php
namespace App\Filament\Pages;
use Auth;
use Filament\Pages\Page;
use Filament\Facades\Filament;
use Illuminate\Support\HtmlString;
use Filament\Forms\Components\Grid;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Forms\Components\Placeholder;
use Phpsa\FilamentPasswordReveal\Password;
use App\Notifications\Admin\PasswordUpdated;
use Illuminate\Validation\ValidationException;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Http\Responses\Auth\Contracts\LoginResponse;
class ChangePassword extends Page implements HasForms
{
use InteractsWithForms;
protected static ?string $navigationGroup = 'Settings';
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static string $view = 'filament.pages.change-password';
public $password, $password_confirmation;
protected function getFormSchema() : array
{
return [
Password::make('password')
->label('New Password')
->revealable()
->minLength(8)
->maxLength(16)
->required()
->confirmed(),
Password::make('password_confirmation')
->label('Confirm Password')
->revealable()
->required()
];
}
public function update()
{
$data = $this->form->getState();
$user = Auth::user();
$user->password = $data['password'];
$user->save();
$user->notify(new PasswordUpdated);
Auth::login($user);
return redirect('admin');
}
}
Hello Sir,
I have the same problem,
this didnt work on me.