feat: 优化 getToken 函数,移除无用的响应参数,调整授权过滤逻辑以支持新路径

This commit is contained in:
2025-12-23 13:15:16 +08:00
parent 4bdebd66d4
commit 371d66b289
3 changed files with 8 additions and 13 deletions

View File

@@ -15,14 +15,9 @@ const cookie = {
return cookies;
}
}
export const getToken = async (req: http.IncomingMessage, res: http.ServerResponse) => {
export const getToken = async (req: http.IncomingMessage) => {
let token = (req.headers?.['authorization'] as string) || (req.headers?.['Authorization'] as string) || '';
const url = new URL(req.url || '', 'http://localhost');
const resNoPermission = () => {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
};
if (!token) {
token = url.searchParams.get('token') || '';
}
@@ -30,9 +25,6 @@ export const getToken = async (req: http.IncomingMessage, res: http.ServerRespon
const parsedCookies = cookie.parse(req.headers.cookie || '');
token = parsedCookies.token || '';
}
if (!token) {
return resNoPermission();
}
if (token) {
token = token.replace('Bearer ', '');
}