How to get currently logged user in AppPanelProvider?

In AppPanelProvider::panel() I have $user = Auth::user();, but the result is NULL i.e. it seems that the currently logged user is not available in this method. Is there a way to have access to it in this method?
6 Replies
nielsdscrd
nielsdscrd2y ago
In AppPanelProvider::panel() you are defining the middleware to be run, including authentication and sessions. Hence at that point the user cannot be known yet.
StanProg
StanProgOP2y ago
I'm defining there the UserMenu, which depends on the currently logged user. Is there a way to setup the UserMenu on another place?
manojkiran
manojkiran2y ago
I haven't started using filament v3 yet so. Does AppPanelProvider extends Filament\PanelProvider ?
manojkiran
manojkiran2y ago
If yes it's not possible to get the current user you can read more from here https://stackoverflow.com/questions/37372357/laravel-how-to-get-current-user-in-appserviceprovider
Stack Overflow
Laravel - How to get current user in AppServiceProvider
So I am usually getting the current user using Auth::user() and when I am determining if a user is actually logged in Auth::check. However this doesn't seem to work in my AppServiceProvider. I am u...
StanProg
StanProgOP2y ago
Is there another way to have a UserMenu with elements based on the currently logged User? For the reference, I did it with closure on hidden():
$userMenuItems[] = MenuItem::make()
->label(__('pages/org.title'))
->url(function() {
return Org::getUrl();
})
->hidden(function() {
return !Auth::user()->hasDirectPermission('manage org');
})
->icon('heroicon-m-building-office');
$userMenuItems[] = MenuItem::make()
->label(__('pages/org.title'))
->url(function() {
return Org::getUrl();
})
->hidden(function() {
return !Auth::user()->hasDirectPermission('manage org');
})
->icon('heroicon-m-building-office');
manojkiran
manojkiran2y ago
is it possible to do this in middleware ? and I think You can access the Gate facade in service provider

Did you find this page helpful?