Laravel Passport Scopes

后端 未结 6 1029
灰色年华
灰色年华 2020-11-28 21:10

I am a bit confused on the laravel scopes part.

I have a user model and table.

How can I assign a user the role of user, customer and/or admin.

I hav

6条回答
  •  星月不相逢
    2020-11-28 21:52

    With @RaymondLagonda solution. If you are getting a class scopes not found error, 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,`
    

    Also, if you are getting the error Type error: Too few arguments to function, you should be able to get the $user from the request like below.

    (I am using laratrust for managing roles)

    public function login(Request $request)
    {
    
        $email = $request->input('username');
        $user = User::where('email','=',$email)->first();
    
        if($user && $user->hasRole('admin')){
            $request->request->add([
                'scope' => 'manage-everything'
            ]);
        }else{
            return response()->json(['message' => 'Unauthorized'],403);
        }
    
        $tokenRequest = Request::create(
          '/oauth/token',
          'post'
        );
    
        return Route::dispatch($tokenRequest);
    
    }
    

提交回复
热议问题