Notification instead of 403 error page

Hi everyone! As I'm working with roles and permissions, I have a lot of action that can and cannot be done depending on the user and its role. Is it possible to get that, instead of triggering a 403 error (from a gate or other policy), a notification with the message "Unauthorized" is triggered? I imagine it can be done by managing Notifications in the various actions, but I would prefer to write it once and have it work for all cases. Thank you very much for the support!
7 Replies
toeknee
toeknee16mo ago
You can intercept it and redirect back to the homepage say and fire out the notification error 403 etc
Davide Cariola
Davide CariolaOP16mo ago
Hi toeknee, thank you for the reply! Where would you code it?
awcodes
awcodes16mo ago
would it not be better to just hide the buttons if the user can't perform the action?
toeknee
toeknee16mo ago
It is talked about here: https://laracasts.com/discuss/channels/laravel/how-to-catch-403-error-to-show-a-flashy-message AS AWCodes says, you would be better to auth()->user()->can()
Laracasts
Davide Cariola
Davide CariolaOP16mo ago
Hi! Yeah, that is the next step. But I'd like to avoid people from forcing its way through the uri. But feel free to tell me if it does make sense or not 😄
awcodes
awcodes16mo ago
Yea, but a policy would handle the uri anyway, right. And the same policy could be used to show or hide the button too.
Davide Cariola
Davide CariolaOP16mo ago
Got it! Thank you so much for your time!

Did you find this page helpful?