koel/app/Http/Middleware/GetUserFromToken.php

30 lines
668 B
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
class GetUserFromToken extends BaseMiddleware
{
/**
* @return mixed
*/
2018-08-24 15:27:19 +00:00
public function handle(Request $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
}
2018-08-24 15:27:19 +00:00
$user = $this->auth->authenticate($token);
2015-12-30 04:14:47 +00:00
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
}
2018-08-24 15:27:19 +00:00
$this->events->dispatch('tymon.jwt.valid', $user);
2015-12-30 04:14:47 +00:00
return $next($request);
}
}