Home > database >  Simple Gate test redirects to unauthorized page all the time
Simple Gate test redirects to unauthorized page all the time

Time:09-26

I have a simple Gate in AuthServiceProvider:

public function boot()
{
    $this->registerPolicies();
 
    Gate::define('test', function (User $user) {
        return true;
    });
}

Then I'm trying to test it on web.php:

Route::get('/', function () {
    return view('welcome');
})->middleware('can:test')

but it redirects me to the unauthorized page, why? The User model exists, but I just return true anyway

CodePudding user response:

Using Gate and can middleware first requires authenticated request, then it check the gate logic you define

If you are getting unauthorized response even if your gate always returns true, it simply means your request came from non-logged-in user

  • Related