I am trying to add JWT using "tymon/jwt-auth": "^1.0"
to Laravel Framework 7.26.1
. Have setup everything as per document. https://jwt-auth.readthedocs.io/en/develop/quick-start/
This is my 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);
}
...
this is api.php
Route::post('login', 'AuthController@login')->middleware('api');
Route::post('logout', 'AuthController@logout');
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
when trying to hit http://127.0.0.1:8000/api/login/
from postman, this is what i am getting
{
"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"
}
]
}
to me it seems like it's trying to using web guard which i don't understand since i am hitting routes in api.php. anyone what mistake i am making ?
Okay. So apparently you have to specify guard in expires_in 'expires_in' => auth('api')->factory()->getTTL() * 60
I updated respondWithToken
Function of AuthController.php
to
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
//'expires_in' => auth()->factory()->getTTL() * 60
'expires_in' => auth('api')->factory()->getTTL() * 60
]);
and it fixed it.
Correct but since you are dealing with an API, you could have changed your default guard from web
to api
in config/auth.php
as per below:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
Then you can use auth without specifying the guard:
'expires_in' => auth()->factory()->getTTL() * 60
You may also need to run php artisan optimize
to clear cache and all should be good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With