feat(server): 移除userservice

This commit is contained in:
dayjoy
2025-09-11 15:38:20 +08:00
parent ddc4ac26bc
commit c782dea322

View File

@@ -1,50 +0,0 @@
import { Request } from "express";
import {extractTokenFromHeader, getUserInfoByToken, UserInfo} from "../middleware/auth";
/**
* User information service class
* Provides user information retrieval methods for direct use in business logic
*/
export class UserService {
/**
* Get user information from request
* If userInfo already exists in request (set by middleware), return it directly
* Otherwise extract token from request header and get user information
*/
static async getUserInfoFromRequest(req: Request): Promise<UserInfo> {
// If middleware has already set user information, return it directly
if (req.userInfo) {
return req.userInfo;
}
// Extract token from request header
const token = extractTokenFromHeader(req.headers.authorization);
if (!token) {
throw new Error("No valid access token found");
}
// Get user information
return await getUserInfoByToken(token);
}
/**
* Verify if user is logged in
*/
static isUserLoggedIn(req: Request): boolean {
return !!req.userInfo;
}
/**
* Get user ID (assuming user info contains id field)
*/
static getUserId(req: Request): string | number | null {
return req.userInfo?.userId || null;
}
/**
* Get user information directly by token (not dependent on request object)
*/
static async getUserInfoByToken(token: string): Promise<any> {
return await getUserInfoByToken(token);
}
}