Skip to content

Instantly share code, notes, and snippets.

@ClaudioHenrique
Created December 22, 2015 03:48
Show Gist options
  • Save ClaudioHenrique/cba7824611fe229caf92 to your computer and use it in GitHub Desktop.
Save ClaudioHenrique/cba7824611fe229caf92 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Middleware\BaseMiddleware;
use JWTAuth;
class JWTAuthNew extends BaseMiddleware
{
/**
* 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', 401);
}
try {
$user = $this->auth->authenticate($token);
} catch (TokenExpiredException $e) {
return $this->respond('tymon.jwt.expired', 'token_expired', 401, [$e]);
} catch (JWTException $e) {
return $this->respond('tymon.jwt.invalid', 'token_invalid', 401, [$e]);
}
$this->events->fire('tymon.jwt.valid', $user);
$this->events->fire('tymon.jwt.user_not_found', $user);
return $next($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment