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
toekneeβ€’9mo ago
You can intercept it and redirect back to the homepage say and fire out the notification error 403 etc
Davide Cariola
Davide Cariolaβ€’9mo ago
Hi toeknee, thank you for the reply! Where would you code it?
awcodes
awcodesβ€’9mo ago
would it not be better to just hide the buttons if the user can't perform the action?
toeknee
toekneeβ€’9mo 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 Cariolaβ€’9mo 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
awcodesβ€’9mo 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 Cariolaβ€’9mo ago
Got it! Thank you so much for your time!