Prevent Logout when changing Admin Password

I created a Page called 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');
}
}
Also, Everytime I hit submit form, it redirects to /login and not /admin/login.
3 Replies
vahnmarty
vahnmartyOP15mo ago
Up
toeknee
toeknee15mo ago
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');
}
}
Chrysippus
Chrysippus7mo ago
Hello Sir, I have the same problem, this didnt work on me.
Want results from more Discord servers?
Add your server