auth()->user() is NULL for loading custom assets with data

Hi there, I am following this docs: https://filamentphp.com/docs/3.x/support/assets#registering-script-data Have added my code to AppServiceProvider boot() method, but auth()->user() is always null. Any ideas?
Solution:
Thanks Dennis, I just created a middleware for it and not it works. Did I missed sth in the docs? It says clearly ServiceProvider boot with this example
Jump to solution
6 Replies
Tonkawuck
Tonkawuck4mo ago
If auth()->user() is null, first make sure you're logged in. Then make sure the authentication middleware is running that sets the session up. I doubt it, but maybe try making your own service provider which runs later and run this logic in there? This help thread might be related to your problem. https://discord.com/channels/883083792112300104/1219034700207620157/1219034700207620157
Patrick
PatrickOP4mo ago
Hey @Tonkawuck , thanks for your reply. I am looged in. I haven't tried the own service provider yet, will do it now. But first will check your linked thread. Thanks Sry I don't get it...I tried custom ServiceProvider, however it does not work. In the docs they write "Sometimes, you may wish to make data from the backend available to JavaScript files. To do this, you can use the FilamentAsset::registerScriptData() method in the boot() method of a service provider:" the best place for my script would be in my AdminPanelProvider boot method. So how can I ensure it only runs after middleware?
Dennis Koch
Dennis Koch4mo ago
Have added my code to AppServiceProvider boot() method, but auth()->user() is always null.
You cannot use auth() in a ServiceProvider directly, because no user is authenticated at this point.
So how can I ensure it only runs after middleware?
Did you try putting it in a middleware?
Solution
Patrick
Patrick4mo ago
Thanks Dennis, I just created a middleware for it and not it works. Did I missed sth in the docs? It says clearly ServiceProvider boot with this example
Dennis Koch
Dennis Koch4mo ago
Did I missed sth in the docs? It says clearly ServiceProvider boot with this example
Yes. But it's clearly not using logic based on the user. It's the easiest point to customise. But special needs require different solutions.
Patrick
PatrickOP4mo ago
Okay, thanks. Custom middleware solved my prob
Want results from more Discord servers?
Add your server