Laravel/Passport 范围问题

Laravel/Passport scope issue

嗨,Whosebug 开发人员。

我遇到了 Laravel/Passport 范围的问题。当我使用带有范围中间件的路由时发生错误。

{
"message": "in_array() expects parameter 2 to be array, null given",
"exception": "ErrorException",
"file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\passport\src\Token.php",
"line": 95,
"trace": [
    {
        "function": "handleError",
        "class": "Illuminate\Foundation\Bootstrap\HandleExceptions",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\passport\src\Token.php",
        "line": 95,
        "function": "in_array"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\passport\src\HasApiTokens.php",
        "line": 54,
        "function": "can",
        "class": "Laravel\Passport\Token",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\passport\src\Http\Middleware\CheckForAnyScope.php",
        "line": 27,
        "function": "tokenCan",
        "class": "App\User",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Laravel\Passport\Http\Middleware\CheckForAnyScope",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Routing\Middleware\SubstituteBindings.php",
        "line": 41,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Routing\Middleware\SubstituteBindings",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
        "line": 59,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Routing\Middleware\ThrottleRequests",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Auth\Middleware\Authenticate.php",
        "line": 44,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Auth\Middleware\Authenticate",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 103,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
        "line": 687,
        "function": "then",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
        "line": 662,
        "function": "runRouteWithinStack",
        "class": "Illuminate\Routing\Router",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
        "line": 628,
        "function": "runRoute",
        "class": "Illuminate\Routing\Router",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
        "line": 617,
        "function": "dispatchToRoute",
        "class": "Illuminate\Routing\Router",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
        "line": 165,
        "function": "dispatch",
        "class": "Illuminate\Routing\Router",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 128,
        "function": "Illuminate\Foundation\Http\{closure}",
        "class": "Illuminate\Foundation\Http\Kernel",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Foundation\Http\Middleware\TransformsRequest",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php",
        "line": 27,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode.php",
        "line": 63,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\fruitcake\laravel-cors\src\HandleCors.php",
        "line": 37,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fruitcake\Cors\HandleCors",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\fideloper\proxy\src\TrustProxies.php",
        "line": 57,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fideloper\Proxy\TrustProxies",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
        "line": 103,
        "function": "Illuminate\Pipeline\{closure}",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
        "line": 140,
        "function": "then",
        "class": "Illuminate\Pipeline\Pipeline",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
        "line": 109,
        "function": "sendRequestThroughRouter",
        "class": "Illuminate\Foundation\Http\Kernel",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\public\index.php",
        "line": 55,
        "function": "handle",
        "class": "Illuminate\Foundation\Http\Kernel",
        "type": "->"
    },
    {
        "file": "C:\Users\amir\PhpstormProjects\baobab_back\server.php",
        "line": 21,
        "function": "require_once"
    }
]

我的路线是:

Route::middleware(["auth:api", "scope:Administrator"])->post('atts/languages', 'AttribsController@createLanguage');

AuthServiceProvider 中的范围定义:

public function boot()
{
    $this->registerPolicies();
    Passport::routes();
    Passport::tokensExpireIn(now()->addCentury());
    Passport::tokensCan([
        'Administrator' => 'Administrator',
        'Moderator' => 'Moderator',
        'Journalist' => 'Journalist',
        'Client' => 'Client'
    ]);
    Passport::setDefaultScope(['Client']);
}

AuthController 中的令牌影响:

 $token = $user->createToken('app', ['Administrator'])->accessToken;

    $user->token = $token;

    return response()->json($user);

有什么解决办法吗?

谢谢。

环境:Windows10OS、Laravel 7.13、Passport 9.2、PHP7.2、数据库MySQL.

更新: AuthController.php

<?php
namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller{
public function signup(Request $request){
$request->validate([
    'email' => 'required|string|unique:users|email',
    'password' => 'required|string',
    "username" => 'required|string|unique:users',
    'name' => 'required|string',
    'typeId' => 'required|int',
    'lName' => 'required|string',
]);

$user = new User();
$user->name = $request->name;
$user->email = $request->email;
$user->password = bcrypt($request->password);
$user->type_id = $request->typeId;
$user->lName = $request->lName;
$user->username = $request->username;
$user->photo = $request->photo;
$user->phone = $request->phone;
$user->address = $request->adr;
$user->bio = $request->bio;

$user->save();

$user = User::with(['type'])->find($user->id);

$token = $user->createToken('app', [$user->type->name])->accessToken;

$user->token = $token;

return response()->json($user);
}

public function login(){
if (!Auth::attempt(['email' => request('email'), "password" =>>request('password')]))
    return response()->json(['message' => 'not acceptable'], Response::HTTP_NOT_ACCEPTABLE);

$user = request()->user();

$user->token = $user->createToken('app', [$user->type->name])->accessToken;

return response()->json($user);
}

public function logout(){
request()->user()->token()->revoke();
return response()->json(['message' => 'goodbye']);
}
}

像这样使用路由:

Route::post('atts/languages', 'AttribsController@createLanguage')->middleware(["auth:api", "scope:Administrator"]);

Kernel.php 在 $routeMiddleware

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

AuthServiceProvider

中使用use Laravel\Passport\Passport;

确保您正在关注所有这些。

已解决,只是删除了 User 模型中的 withAccessToken($accessToken) 方法(不知道它的来源)。