feat: add authentication routes, update environment variables, and enhance error handling
This commit is contained in:
28
Backend/utils/jwt.ts
Normal file
28
Backend/utils/jwt.ts
Normal 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;
|
||||
}
|
||||
11
Backend/utils/password.ts
Normal file
11
Backend/utils/password.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
|
||||
const SALT_ROUNDS = 12;
|
||||
|
||||
export async function hashPassword(password: string): Promise<string> {
|
||||
return bcrypt.hash(password, SALT_ROUNDS);
|
||||
}
|
||||
|
||||
export async function verifyPassword(password: string, hash: string): Promise<boolean> {
|
||||
return bcrypt.compare(password, hash);
|
||||
}
|
||||
Reference in New Issue
Block a user