This commit is contained in:
2026-01-17 23:11:08 +08:00
parent d6da542989
commit 7b0b00feff
9 changed files with 95 additions and 184 deletions

View File

@@ -20,27 +20,27 @@ export const getTokenFromRequest = (req: http.IncomingMessage) => {
}
return token;
}
export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
const token = getTokenFromRequest(req);
const resNoPermission = () => {
res.statusCode = 401;
res.end(error('Invalid authorization'));
return { tokenUser: null, token: null };
};
if (!token) {
return resNoPermission();
}
let tokenUser;
try {
tokenUser = await User.verifyToken(token);
} catch (e) {
console.log('checkAuth error', e);
res.statusCode = 401;
res.end(error('Invalid token'));
return { tokenUser: null, token: null };
}
return { tokenUser, token };
};
// export const checkAuth = async (req: http.IncomingMessage, res: http.ServerResponse) => {
// const token = getTokenFromRequest(req);
// const resNoPermission = () => {
// res.statusCode = 401;
// res.end(error('Invalid authorization'));
// return { tokenUser: null, token: null };
// };
// if (!token) {
// return resNoPermission();
// }
// let tokenUser;
// try {
// tokenUser = await User.verifyToken(token);
// } catch (e) {
// console.log('checkAuth error', e);
// res.statusCode = 401;
// res.end(error('Invalid token'));
// return { tokenUser: null, token: null };
// }
// return { tokenUser, token };
// };
export const getLoginUserByToken = async (token: string) => {
if (token) {