F
Filament11mo ago
DGINX

auth()->user() everytime null

Hello, i'm new at filament. I'm not sure but i read multiple times that i can't use auth()->user() function because it's every time null, i have to use instead Filament::auth()->user() . I created a custom admin guard with the Model SystemUser. The login works fine with them. My Question is: Is it really true, that i cannot use auth()->user(), when i use a custom guard? Because i installed an Plugin https://filamentphp.com/plugins/shuvroroy-spatie-laravel-backup. And inside his code https://github.com/shuvroroy/filament-spatie-laravel-backup/blob/main/src/Components/BackupDestinationListRecords.php on Line 78 he used auth()->user()->can('delete-backup'). But this throws an error, because auth()->user() is still null. If i use no custom guards, just the regular User Model, everything works fine. The Plugin works without problems and when i call auth()->user() it's no more null. Here my Code. config/auth.php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'admin' => [
'driver' => 'session',
'provider' => 'admin'
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],

'admin' => [
'driver' => 'eloquent',
'model' => App\Models\SystemUser::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'admin' => [
'driver' => 'session',
'provider' => 'admin'
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],

'admin' => [
'driver' => 'eloquent',
'model' => App\Models\SystemUser::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_reset_tokens',
'expire' => 60,
'throttle' => 60,
],
],
'password_timeout' => 10800,
];
1 Reply
DGINX
DGINXOP11mo ago
AdminPanelProvider.php
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->font('JetBrains Mono', provider: BunnyFontProvider::class)
->login()
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
//Widgets\FilamentInfoWidget::class,
])
->plugins([
FilamentSpatieLaravelHealthPlugin::make()
->usingPage(HealthCheckResults::class),
FilamentSpatieLaravelBackupPlugin::make()
->usingPage(BackupsResults::class)
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
AdminMiddleware::class,

DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authGuard('admin')
->authMiddleware([
AdminAuthenticatedMiddleware::class,
]);
}
public function panel(Panel $panel): Panel
{
return $panel
->default()
->id('admin')
->path('admin')
->font('JetBrains Mono', provider: BunnyFontProvider::class)
->login()
->colors([
'primary' => Color::Amber,
])
->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
->pages([
Pages\Dashboard::class,
])
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
Widgets\AccountWidget::class,
//Widgets\FilamentInfoWidget::class,
])
->plugins([
FilamentSpatieLaravelHealthPlugin::make()
->usingPage(HealthCheckResults::class),
FilamentSpatieLaravelBackupPlugin::make()
->usingPage(BackupsResults::class)
])
->middleware([
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
AdminMiddleware::class,

DisableBladeIconComponents::class,
DispatchServingFilamentEvent::class,
])
->authGuard('admin')
->authMiddleware([
AdminAuthenticatedMiddleware::class,
]);
}
AdminMiddleware.php
public function handle(Request $request, Closure $next)
{
Auth::setDefaultDriver('admin');

return $next($request);

}
public function handle(Request $request, Closure $next)
{
Auth::setDefaultDriver('admin');

return $next($request);

}
AdminAuthenticatedMiddleware.php
public function handle(Request $request, Closure $next, $guard = null)
{
Auth::setDefaultDriver('admin');
if (!Auth::check()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect(route('filament.admin.auth.login', ['return_url' => $request->url()]));
}
}
return $next($request);

}
public function handle(Request $request, Closure $next, $guard = null)
{
Auth::setDefaultDriver('admin');
if (!Auth::check()) {
if ($request->ajax() || $request->wantsJson()) {
return response('Unauthorized.', 401);
} else {
return redirect(route('filament.admin.auth.login', ['return_url' => $request->url()]));
}
}
return $next($request);

}
Want results from more Discord servers?
Add your server