antonioribeiro / firewall

Firewall package for Laravel applications
BSD 3-Clause "New" or "Revised" License
1.39k stars 164 forks source link

Is it possible to return a json response on blacklisted routes? #107

Open n0490b opened 6 years ago

n0490b commented 6 years ago

I am using laravel as my backend and I want to block users from accessing certain routes. However, I always get a 404 page not found when I add a route to the 'redirect_to' even when I use your coming/soon example. What am I doing wrong?

config file

    'responses' => [
        'blacklist' => [
            'code' => 403, // 200 = log && notify, but keep pages rendering

            'message' => null,

            'view' => null,

            'redirect_to' => 'authorize/user',

            'abort' => false, // return abort() instead of Response::make() - disabled by default
        ],

in my api routes

Route::get('authorize/user', [
    'uses' => 'Auth\LoginController@test'
]);

Route::group(['middleware' => 'fw-block-blacklisted'], function ()
{

    Route::post('test/', [
        'uses' => 'LoginController@test'
    ]);
});

controller

    public function test()
    {
        return response()->json(['error' => ' An error has occurred please try again later'], 400);
    }
n0490b commented 6 years ago

Ok I just figured it out. Its using the web routes file so I moved my authorize/user route to web.php.

Route::get('authorize/user', [
    'uses' => 'Auth\LoginController@userIsBanned'
]);

Is there anyway to specify to use the api.php file?

Thanks for the package btw its great!

mwkcoding commented 6 years ago

@n0490b I know it's an old issue but I wanted to answer it anyway in case other people has this issue, or you still have this problem.

If you want to use the route as an API route, you just set the config to redirect to api/authorize/user Since it's an API route, Laravel is made this way to differentiate between to the two types of routes, so all API routes has the api/ prefix.