koel/app/Http/Middleware/GetUserFromToken.php

43 lines
1.1 KiB
PHP
Raw Normal View History

2015-12-30 04:14:47 +00:00
<?php
namespace App\Http\Middleware;
2016-04-02 13:16:09 +00:00
use Closure;
2017-06-04 01:30:45 +00:00
use Illuminate\Http\Request;
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.
*
2017-06-04 01:30:45 +00:00
* @param Request $request
2017-06-04 01:31:59 +00:00
* @param Closure $next
2016-01-03 11:32:38 +00:00
*
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()) {
return $this->respond('tymon.jwt.absent', 'token_not_provided', 401);
2015-12-30 04:14:47 +00:00
}
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) {
return $this->respond('tymon.jwt.user_not_found', 'user_not_found', 401);
2015-12-30 04:14:47 +00:00
}
$this->events->fire('tymon.jwt.valid', $user);
return $next($request);
}
}