v2board/app/Http/Middleware/User.php

31 lines
744 B
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Middleware;
2022-12-13 12:29:23 +08:00
use App\Services\AuthService;
2019-10-29 15:33:36 +08:00
use Closure;
2022-07-19 03:11:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-10-29 15:33:36 +08:00
class User
{
/**
* Handle an incoming request.
*
2020-01-11 13:36:52 +08:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
2019-10-29 15:33:36 +08:00
* @return mixed
*/
public function handle($request, Closure $next)
{
2021-08-05 13:48:24 +08:00
$authorization = $request->input('auth_data') ?? $request->header('authorization');
2022-07-11 14:48:35 +08:00
if (!$authorization) abort(403, '未登录或登陆已过期');
2022-12-13 12:29:23 +08:00
$user = AuthService::decryptAuthData($authorization);
if (!$user) abort(403, '未登录或登陆已过期');
2022-07-11 14:48:35 +08:00
$request->merge([
2022-12-13 12:29:23 +08:00
'user' => $user
2022-07-11 14:48:35 +08:00
]);
2019-10-29 15:33:36 +08:00
return $next($request);
}
}