fix:修复部分 token 的过期异常未被 catch 的问题

This commit is contained in:
妙码生花 2024-03-28 16:27:02 +08:00
parent 8c8f0447a3
commit f748f60e27
2 changed files with 19 additions and 11 deletions

View File

@ -5,9 +5,9 @@ namespace app\api\controller;
use ba\Tree; use ba\Tree;
use Throwable; use Throwable;
use think\facade\Db; use think\facade\Db;
use think\facade\Cookie;
use think\facade\Config; use think\facade\Config;
use app\common\controller\Frontend; use app\common\controller\Frontend;
use app\common\library\token\TokenExpirationException;
class Index extends Frontend class Index extends Frontend
{ {
@ -42,12 +42,15 @@ class Index extends Frontend
// 若是从前台会员中心内发出的请求,要求必须登录,否则会员中心异常 // 若是从前台会员中心内发出的请求,要求必须登录,否则会员中心异常
$requiredLogin = $this->request->get('requiredLogin/b', false); $requiredLogin = $this->request->get('requiredLogin/b', false);
if ($requiredLogin) { if ($requiredLogin) {
$token = $this->request->server('HTTP_BA_USER_TOKEN', $this->request->request('ba-user-token', Cookie::get('ba-user-token') ?: false));
// 若过期可触发过期判定 // 触发可能的 token 过期异常
$this->auth->init($token); try {
$token = get_auth_token(['ba', 'user', 'token']);
$this->auth->init($token);
} catch (TokenExpirationException) {
$this->error(__('Token expiration'), [], 409);
}
// 未过期或无token直接要求登录
$this->error(__('Please login first'), [ $this->error(__('Please login first'), [
'type' => $this->auth::NEED_LOGIN 'type' => $this->auth::NEED_LOGIN
], $this->auth::LOGIN_RESPONSE_CODE); ], $this->auth::LOGIN_RESPONSE_CODE);

View File

@ -14,10 +14,10 @@ namespace ba;
use Throwable; use Throwable;
use think\Response; use think\Response;
use think\facade\Config; use think\facade\Config;
use think\facade\Cookie;
use app\admin\library\Auth; use app\admin\library\Auth;
use app\admin\library\module\Manage; use app\admin\library\module\Manage;
use think\exception\HttpResponseException; use think\exception\HttpResponseException;
use app\common\library\token\TokenExpirationException;
class Terminal class Terminal
{ {
@ -190,11 +190,16 @@ class Terminal
} }
if ($authentication) { if ($authentication) {
$token = request()->server('HTTP_BATOKEN', request()->request('batoken', Cookie::get('batoken') ?: false)); try {
$auth = Auth::instance(); $token = get_auth_token();
$auth->init($token); $auth = Auth::instance();
if (!$auth->isLogin() || !$auth->isSuperAdmin()) { $auth->init($token);
$this->execError("You are not super administrator or not logged in", true);
if (!$auth->isLogin() || !$auth->isSuperAdmin()) {
$this->execError("You are not super administrator or not logged in", true);
}
} catch (TokenExpirationException) {
$this->execError(__('Token expiration'));
} }
} }