简体   繁体   中英

How to properly access a scope in laravel?

I'm currently using Laravel Passport, and I can verify that there is current token saved by using localhost:8000/api/check that returns json below:

{"id":"1c080ff73c6592b8e35630ae36f45f5042c04d9a9ed26a7fafc3793c606484b619ed8792be65a658","user_id":1,"client_id":5,"name":"Personal Access Tokens","scopes":["administrator"],...} 

But when I tried to use middleware scope for admin using localhost:8000/api/admin it returns an error

Illuminate\Contracts\Container\BindingResolutionException: Target class [scope] does not exist. in file


Here is the routes/api.php

Route::group(['middleware' => 'auth:api'], function(){
    Route::get('check', 'TeamController@check');

    Route::group(['middleware' => 'scope:administrator'], function() {
        Route::get('admin', 'TeamController@index');
    });
});

Here's the corresponding functions on TeamController.php

public function check(Request $request) {
    return auth()->user()->token();
}

public function index(Request $request) {
    return auth()->user()->token();
}

Somebody knows what I went wrong?

You most likely did not register the scope middleware.

"Passport includes two middleware that may be used to verify that an incoming request is authenticated with a token that has been granted a given scope. To get started, add the following middleware to the $routeMiddleware property of your app/Http/Kernel.php file:"

'scopes' => \Laravel\Passport\Http\Middleware\CheckScopes::class,
'scope' => \Laravel\Passport\Http\Middleware\CheckForAnyScope::class,

Laravel 7.x Docs - Passport - Checking Scopes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM