Downloads
Stars
Version
composer require codeforyou/laravel-jwt
Codeforyou\Auth\Providers\JwtProvider::class,
php artisan vendor:publish --provider="Codeforyou\Auth\Providers\JwtProvider"
'jwt' => \Codeforyou\Auth\Middleware\JwtMiddleware::class,
<?php
return [
'secret' => substr(explode(':', env('APP_KEY'))[1], 0, 32),
'expires_in' => 7200,
'auth' => [
'model' => App\User::class,
'primary_key' => 'id'
]
];
use Codeforyou\Auth\JwtAuth;
use App\User;
$user = User::find(1);
$token = JwtAuth::token($user);
{
"Authorization": "jwt eyJ0eXAiOiJqd3QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwiaWF0IjoxNTM0MDUzNzQ4fQ.Hn8ZvcJyY0nu8s2fkVeRf3TYt4Up1HtGpIqoC9gLUjs"
}
Route::middleware(['jwt'])->group(function () {
Route::get('/test2', 'TestController@test2');
});
$payload = JwtAuth::payload();
$user = JwtAuth::user();
$id = JwtAuth::id();
you can copy below code to your Laravel app/Exceptions/handler.php render function;
it's easy to change the token exception for us;
it's easy to change the return data for api response.
of course, we should:
use Codeforyou\Auth\Exceptions\TokenExpireException;
use Codeforyou\Auth\Exceptions\NoAuthorizationException;
use Codeforyou\Auth\Exceptions\AuthModelException;
use Codeforyou\Jwt\JwtException;
if ($exception instanceof TokenExpireException) {
return response()->json([
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'data' => ''
]);
}
if ($exception instanceof NoAuthorizationException) {
return response()->json([
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'data' => ''
]);
}
if ($exception instanceof AuthModelException) {
return response()->json([
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'data' => ''
]);
}
if ($exception instanceof JwtException) {
return response()->json([
'code' => $exception->getCode(),
'message' => $exception->getMessage(),
'data' => ''
]);
}