config/auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
routes/api.php
Route::post('auth/login', 'Auth\JwtAuthController@login');
Route::group(['middleware' => 'api','prefix' => 'auth'], function () {
Route::post('logout', 'Auth\JwtAuthController@logout');
Route::post('refresh', 'Auth\JwtAuthController@refresh');
Route::post('me', 'Auth\JwtAuthController@me');
});
AuthController.php
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
class JwtAuthController extends Controller
{
public function login() {
$credentials = request(['email', 'password']);
if (! $token = auth('api')->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
public function logout() {
auth('api')->logout();
return response()->json(['message' => 'Successfully logged out']);
}
public function refresh() {
return $this->respondWithToken(auth('api')->refresh());
}
protected function respondWithToken($token) {
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth('api')->factory()->getTTL() * 60
]);
}
public function me() {
return response()->json(auth('api')->user());
}
}
@SouKBit per the docs, you need to adjust your auth.php like so:
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
From the docs:
Multiple Guards
If the newly created 'api' guard is not set as a default guard
or you have defined multiple guards to handle authentication,
you should specify the guard when calling auth().
$token = auth('api')->attempt($credentials);
Add "implements JWTSubject" in your user class.
class User extends Authenticatable implements JWTSubject
Solved for me, trying for this example:
'api' => [
'driver' => 'jwt',
'provider' => 'users',
],
That line is the problem for the laravel 5.8 ^.
'hash' => false,
If you confirm there are no wrong on your project to implement jwt that time you can do it...
php artisan config:cache
php artisan cache:clear
php artisan route:clear
.. it's work for me...
implements JWTSubject
and use Tymon\JWTAuth\Contracts\JWTSubject;
eg
...
use Tymon\JWTAuth\Contracts\JWTSubject;class User extends Authenticatable implements JWTSubject
{
...
Most helpful comment
From the docs: