49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of jwt-auth.
|
|
*
|
|
* (c) Sean Tymon <tymon148@gmail.com>
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Tymon\JWTAuth\Middleware;
|
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
|
|
|
class GetUserFromToken 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', 400);
|
|
}
|
|
|
|
try {
|
|
$user = $this->auth->authenticate($token);
|
|
} catch (TokenExpiredException $e) {
|
|
return $this->respond('tymon.jwt.expired', 'token_expired', 200, [$e]);
|
|
} catch (JWTException $e) {
|
|
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);
|
|
}
|
|
}
|