Laravel redirect on unauthorized action

327 Views Asked by At

I'm using Laravel 7 and appzcoder/laravel-admin package. my system is already pretty extensive and I have been able to work with authentication, authorization, roles and permissions fine so far. Today when my users try to do an action and they don't have authorization for it laravel gates and polices throw an unathorized exception and redirect them to a 403 not authorized page.

What I wish was that everytime my users don't have permission, laravel redirected them back with a message. How can I do that?

Remembering that My Guards and Polices are auto-generated with my permissions table rows as per appzcoder/laravel-admin functionality.

1

There are 1 best solutions below

0
Santosh Dangare On

This may be the problem of CSRF token
CSRF is enabled by default on all Routes in Laravel > 5, you can disable it for specific routes by modifying app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends BaseVerifier
{
    protected $except = [
        // Place your URIs here
    ];
}

place all your unauthorized routes in this file seperated by comma.
example :

protected $except = [
    'users/get_some_info',
];