feat: add authentication routes, update environment variables, and enhance error handling

This commit is contained in:
2025-12-07 13:47:00 +00:00
parent 08aefd7cbe
commit df2b9e56b4
9 changed files with 261 additions and 4 deletions

28
Backend/utils/jwt.ts Normal file
View File

@@ -0,0 +1,28 @@
import jwt from 'jsonwebtoken';
const JWT_EXPIRES_IN = (process.env.JWT_EXPIRES_IN ?? '7d') as jwt.SignOptions['expiresIn'];
function getJwtSecret(): string {
const secret = process.env.JWT_SECRET;
if (!secret) {
throw new Error('JWT_SECRET is not set');
}
return secret;
}
export type JwtPayload = {
userId: string;
email: string;
};
export function signAccessToken(payload: JwtPayload): string {
return jwt.sign(payload, getJwtSecret(), {
expiresIn: JWT_EXPIRES_IN,
});
}
export function verifyAccessToken(token: string): JwtPayload {
return jwt.verify(token, getJwtSecret()) as JwtPayload;
}