feat: Docker部署与CI/CD集成, 搜索栏修复, 上传目录改为data

This commit is contained in:
xiner
2025-11-28 18:42:30 +08:00
commit 8351d6bbfc
243 changed files with 13192 additions and 0 deletions

48
lib/auth.ts Normal file
View File

@@ -0,0 +1,48 @@
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
const SALT_ROUNDS = 10;
function getJwtSecret(): string {
const secret = process.env.JWT_SECRET;
if (!secret) {
throw new Error('JWT_SECRET is not set');
}
return secret;
}
/**
* Hash a password using bcrypt
*/
export async function hashPassword(password: string): Promise<string> {
return bcrypt.hash(password, SALT_ROUNDS);
}
/**
* Verify a password against a hash
*/
export async function verifyPassword(password: string, hash: string): Promise<boolean> {
return bcrypt.compare(password, hash);
}
/**
* Generate a JWT token for a user
*/
export function generateToken(userId: string): string {
return jwt.sign({ userId }, getJwtSecret(), {
expiresIn: '7d'
});
}
/**
* Verify and decode a JWT token
* Returns the user ID if valid, null otherwise
*/
export function verifyToken(token: string): { userId: string } | null {
try {
const decoded = jwt.verify(token, getJwtSecret()) as { userId: string };
return decoded;
} catch (error) {
return null;
}
}