2015-12-30 04:14:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
2016-04-02 13:16:09 +00:00
|
|
|
use Closure;
|
2015-12-30 04:14:47 +00:00
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
|
|
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
|
|
|
|
|
|
|
|
class GetUserFromToken extends BaseMiddleware
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
2016-01-03 11:32:38 +00:00
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
*
|
2015-12-30 04:14:47 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-04-02 13:16:09 +00:00
|
|
|
public function handle($request, Closure $next)
|
2015-12-30 04:14:47 +00:00
|
|
|
{
|
2016-01-03 11:32:38 +00:00
|
|
|
if (!$token = $this->auth->setRequest($request)->getToken()) {
|
2015-12-30 04:14:47 +00:00
|
|
|
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', $e->getStatusCode(), [$e]);
|
|
|
|
} catch (JWTException $e) {
|
|
|
|
return $this->respond('tymon.jwt.invalid', 'token_invalid', $e->getStatusCode(), [$e]);
|
|
|
|
}
|
|
|
|
|
2016-01-03 11:32:38 +00:00
|
|
|
if (!$user) {
|
2015-12-30 04:14:47 +00:00
|
|
|
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 404);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->events->fire('tymon.jwt.valid', $user);
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|