Hi, I am using laravel web service and when token is expired web service return an error like
{"error":"token_expired"} but i want to generate message like {"succes":0,"error":"token expired, please login again."}
Create your own version of the middleware, change this line for jwt.auth and this one for jwt.refresh, and apply them to your routes to replace the originals.
ok, but i can change the message but how to include the success = 0 part
return $this->respond('tymon.jwt.expired', 'token expired, please login again.', $e->getStatusCode(), [$e]);
Bruh, it seems like you just need to familiarize yourself further with Laravel. Cause the question your asking doesn't seem to be specific to JWT. Overriding exceptions and handling them in a way that you want is built into Laravel.
Personally I think this should be implemented in your Handler.php file like this:
if ($e instanceof Tymon\JWTAuth\Exceptions\TokenExpiredException) {
return response()->json(['token_expired'], $e->getStatusCode());
}
But it seems that you want specifics so it'd be something like this:
if ($e instanceof Tymon\JWTAuth\Exceptions\TokenExpiredException) {
return response()->json(['success' => 0, 'error' => 'token_expired'], $e->getStatusCode());
}
The specifics of where you put this, if you're not familiar, can be found here: https://laravel.com/docs/5.1/errors
@mallardduck, @tdhsmith Thanks
You can create your own middleware e.g JWTAuth then copy code from Tymon\JWTAuth\Middleware\GetUserFromToken like this
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Middleware\GetUserFromToken;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
class JWTCheck extends GetUserFromToken
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, \Closure $next)
{
if (! $token = $this->auth->setRequest($request)->getToken()) {
return $this->respond('tymon.jwt.absent', 'token_not_provided', 400);
}
try {
$user = $this->auth->authenticate($token);
} catch (TokenExpiredException $e) {
return response()->json([
'meta' => [
'code' => 401,
'error_type' => 'token_expired',
'error_message' => 'Please provide the correct credentials'
]], 401);
//return $this->respond('tymon.jwt.expired', 'token_expired', $e->getStatusCode(), [$e]);
} catch (JWTException $e) {
return response()->json([
'meta' => [
'code' => 401,
'error_type' => 'token_invalid',
'error_message' => 'Please provide the correct token'
]], 401);
//return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
}
if (! $user) {
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
}
$this->events->fire('tymon.jwt.valid', $user);
return $next($request);
}
}
then change middleware in Kernel.php to your custom middleware.
Most helpful comment
Bruh, it seems like you just need to familiarize yourself further with Laravel. Cause the question your asking doesn't seem to be specific to JWT. Overriding exceptions and handling them in a way that you want is built into Laravel.
Personally I think this should be implemented in your Handler.php file like this:
But it seems that you want specifics so it'd be something like this:
The specifics of where you put this, if you're not familiar, can be found here: https://laravel.com/docs/5.1/errors