“ message”:“方法Illuminate \\ Auth \\ SessionGuard :: factory不存在。”,

时间:2020-08-29 12:32:00

标签: php laravel jwt

我正在尝试使用"tymon/jwt-auth": "^1.0"将JWT添加到Laravel Framework 7.26.1。按照文档设置所有内容。 https://jwt-auth.readthedocs.io/en/develop/quick-start/

这是我的AuthController.php

<?php 

namespace App\Http\Controllers;
    
    use Illuminate\Support\Facades\Auth;
    use App\Http\Controllers\Controller;
    
    class AuthController extends Controller
    {
        /**
         * Create a new AuthController instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth:api', ['except' => ['login']]);
        }
    
        /**
         * Get a JWT via given credentials.
         *
         * @return \Illuminate\Http\JsonResponse
         */
        public function login()
        {
    
            $credentials = request(['email', 'password']);
    
            if (! $token = Auth::attempt($credentials)) {
                return response()->json(['error' => 'Unauthorized'], 401);
            }
    
            return $this->respondWithToken($token);
        }
...

这是api.php

Route::post('login', 'AuthController@login')->middleware('api');

Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');

当我试图从邮递员打http://127.0.0.1:8000/api/login/时,这就是我所得到的

{
"message": "Method Illuminate\\Auth\\SessionGuard::factory does not exist.",
"exception": "BadMethodCallException",
"file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php",
"line": 103,
"trace": [
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php",
        "line": 307,
        "function": "__call",
        "class": "Illuminate\\Auth\\SessionGuard",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/app/Http/Controllers/AuthController.php",
        "line": 81,
        "function": "__call",
        "class": "Illuminate\\Auth\\AuthManager",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/app/Http/Controllers/AuthController.php",
        "line": 34,
        "function": "respondWithToken",
        "class": "App\\Http\\Controllers\\AuthController",
        "type": "->"
    },
    {
        "function": "login",
        "class": "App\\Http\\Controllers\\AuthController",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
        "line": 54,
        "function": "call_user_func_array"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
        "line": 45,
        "function": "callAction",
        "class": "Illuminate\\Routing\\Controller",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
        "line": 239,
        "function": "dispatch",
        "class": "Illuminate\\Routing\\ControllerDispatcher",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
        "line": 196,
        "function": "runController",
        "class": "Illuminate\\Routing\\Route",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 685,
        "function": "run",
        "class": "Illuminate\\Routing\\Route",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 128,
        "function": "Illuminate\\Routing\\{closure}",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
        "line": 41,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
        "line": 59,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 687,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 662,
        "function": "runRouteWithinStack",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 628,
        "function": "runRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
        "line": 617,
        "function": "dispatchToRoute",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 165,
        "function": "dispatch",
        "class": "Illuminate\\Routing\\Router",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 128,
        "function": "Illuminate\\Foundation\\Http\\{closure}",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
        "line": 21,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
        "line": 27,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php",
        "line": 63,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/fruitcake/laravel-cors/src/HandleCors.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fruitcake\\Cors\\HandleCors",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/fideloper/proxy/src/TrustProxies.php",
        "line": 57,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 167,
        "function": "handle",
        "class": "Fideloper\\Proxy\\TrustProxies",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
        "line": 103,
        "function": "Illuminate\\Pipeline\\{closure}",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 140,
        "function": "then",
        "class": "Illuminate\\Pipeline\\Pipeline",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
        "line": 109,
        "function": "sendRequestThroughRouter",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/public/index.php",
        "line": 55,
        "function": "handle",
        "class": "Illuminate\\Foundation\\Http\\Kernel",
        "type": "->"
    },
    {
        "file": "/Applications/XAMPP/xamppfiles/htdocs/laraval-certificate/arc.dev/server.php",
        "line": 21,
        "function": "require_once"
    }
]

}

在我看来,这似乎是尝试使用网络警卫,我不明白,因为我在api.php中遇到路由。任何人我犯什么错误?

2 个答案:

答案 0 :(得分:7)

正确,但由于您正在处理 API,您可以将 web 中的默认保护从 api 更改为 config/auth.php,如下所示:

'defaults' => [
        'guard' => 'api',
        'passwords' => 'users',
],

然后你可以使用auth而不指定guard: 'expires_in' => auth()->factory()->getTTL() * 60

您可能还需要运行 php artisan optimize 来清除缓存,一切都应该没问题。

答案 1 :(得分:3)

好的。因此,显然您必须在'expires_in' => auth('api')->factory()->getTTL() * 60

的expires_in中指定警卫

我将respondWithToken的{​​{1}}功能更新为

AuthController.php

并将其修复。