TaoLer/app/middleware/Auth.php

32 lines
571 B
PHP
Raw Normal View History

2020-01-01 13:17:19 +08:00
<?php
namespace app\middleware;
2024-04-01 10:04:16 +08:00
use app\common\lib\JwtAuth;
2020-01-01 13:17:19 +08:00
class Auth
{
public function handle($request, \Closure $next)
{
2024-04-01 10:04:16 +08:00
$header = $request->header();
2021-12-15 15:46:04 +08:00
2024-04-01 10:04:16 +08:00
if(isset($header['authorization'])) {
$token = trim(ltrim($request->header('authorization'), 'Bearer'));
2021-12-15 15:46:04 +08:00
2024-04-01 10:04:16 +08:00
try{
$data = JwtAuth::decode($token);
$request->uid = $data->uid;
} catch(\Exception $e) {
2024-04-01 10:24:38 +08:00
return json(['code' => -1, 'msg' => $e->getMessage()]);
2020-01-01 13:17:19 +08:00
}
2024-04-01 10:04:16 +08:00
} else {
return json(['code' => -1, 'msg' => 'no auth']);
2020-11-01 18:13:05 +08:00
}
2024-04-01 10:04:16 +08:00
return $next($request);
2020-01-01 13:17:19 +08:00
}
}