Skip to content

Instantly share code, notes, and snippets.

@gpressutto5
Created December 20, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gpressutto5/632afb1275f8aa13f135f3734ff65f8c to your computer and use it in GitHub Desktop.
Save gpressutto5/632afb1275f8aa13f135f3734ff65f8c to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Middleware;
use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\JWTAuth;
class RefreshTokenIfLoggedIn
{
/**
* The JWT Authenticator.
*
* @var \Tymon\JWTAuth\JWTAuth
*/
protected $auth;
/**
* Create a new RefreshTokenIfLoggedIn instance.
*
* @param \Tymon\JWTAuth\JWTAuth $auth
*
* @return void
*/
public function __construct(JWTAuth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
if (! $this->auth->parser()->setRequest($request)->hasToken()) {
return $response;
}
try {
$token = $this->auth->parseToken()->refresh();
} catch (JWTException $e) {
return $response;
}
$response->headers->set('Authorization', 'Bearer '.$token);
return $response;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment