JwtMiddleware.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Http\Middleware;
  3. use Closure;
  4. use JWTAuth;
  5. use Exception;
  6. use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
  7. class JwtMiddleware extends BaseMiddleware
  8. {
  9. /**
  10. * Handle an incoming request.
  11. *
  12. * @param \Illuminate\Http\Request $request
  13. * @param \Closure $next
  14. * @return mixed
  15. */
  16. public function handle($request, Closure $next)
  17. {
  18. try {
  19. $user = JWTAuth::parseToken()->authenticate();
  20. } catch (Exception $e) {
  21. if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenInvalidException){
  22. return response()->json(['status' => 'Token is Invalid'],400);
  23. }else if ($e instanceof \Tymon\JWTAuth\Exceptions\TokenExpiredException){
  24. return response()->json(['status' => 'Token is Expired'],401);
  25. }else{
  26. return response()->json(['status' => 'Authorization Token not found'],400);
  27. }
  28. }
  29. return $next($request);
  30. }
  31. }