You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
810 B
32 lines
810 B
<?php
|
|
|
|
namespace app\api\middleware;
|
|
|
|
use app\api\model\User;
|
|
use app\api\service\LcJWTService;
|
|
use app\Request;
|
|
|
|
class LcJWTAuth
|
|
{
|
|
public function handle(Request $request, \Closure $next)
|
|
{
|
|
$token = $request->header('token');
|
|
|
|
if (!$token) {
|
|
return json(['code' => 401, 'msg' => 'token cannot be empty']);
|
|
}
|
|
|
|
$data = LcJWTService::parseToken($token);
|
|
$claims = $data['claims'];
|
|
|
|
$validateRes = LcJWTService::validationToken($token, $claims->get('uid'));
|
|
|
|
if ($validateRes !== true) {
|
|
return json(['code' => 401, 'msg' => 'token verification failed']);
|
|
}
|
|
|
|
$request->uid = $claims->get('uid');
|
|
$request->user_info = User::where('uid', $request->uid)->find();
|
|
return $next($request);
|
|
}
|
|
}
|
|
|