koel/app/Http/Middleware/GetUserFromToken.php

41 lines
1.1 KiB
PHP
Raw Normal View History

2015-12-30 04:14:47 +00:00
<?php
namespace App\Http\Middleware;
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
*/
public function handle($request, \Closure $next)
{
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);
}
}