F
Filamentmberneis

How do I programatically log into filament?

My Filament has resource pages for admins and non-admins. - I have a user resource and a table-action to impersonate users. I am using this code:
Tables\Actions\Action::make('Impersonate')
->icon('heroicon-o-user')
->color('info')
->action(function (User $user) {
Filament::auth()->login($user);
Filament::auth()->setUser($user);
return redirect()->route('filament.app.pages.home');
})
Tables\Actions\Action::make('Impersonate')
->icon('heroicon-o-user')
->color('info')
->action(function (User $user) {
Filament::auth()->login($user);
Filament::auth()->setUser($user);
return redirect()->route('filament.app.pages.home');
})
The Home page is a guest page and not authenticated - I can see in that page the user avatar and my badges in the navigation bar calculated from the current user. - But once I click on a regular Filament page I get the login panel. - What else do I need to do to make Filament switch to the new user?
dissto
disstoβ€’15d ago
Just fyi there is plugin for that though.
GitHub
GitHub - stechstudio/filament-impersonate: Filament plugin that mak...
Filament plugin that makes it easy to impersonate your users - stechstudio/filament-impersonate
mberneis
mberneisβ€’15d ago
Thanks - I will try it. - Still curious how to achieve this but the plugin's source code might show me how πŸ˜‰