Add complete authentication infrastructure including: - Password hashing utilities with bcrypt - JWT token generation and verification - Auth service with register, login, refresh, and logout - Auth controller with Zod validation - Auth middleware for route protection - Auth routes mounted at /api/auth Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
28 lines
754 B
TypeScript
28 lines
754 B
TypeScript
import jwt from 'jsonwebtoken';
|
|
import type { JWTPayload } from '@horux/shared';
|
|
import { env } from '../config/env.js';
|
|
|
|
export function generateAccessToken(payload: Omit<JWTPayload, 'iat' | 'exp'>): string {
|
|
return jwt.sign(payload, env.JWT_SECRET, {
|
|
expiresIn: env.JWT_EXPIRES_IN,
|
|
});
|
|
}
|
|
|
|
export function generateRefreshToken(payload: Omit<JWTPayload, 'iat' | 'exp'>): string {
|
|
return jwt.sign(payload, env.JWT_SECRET, {
|
|
expiresIn: env.JWT_REFRESH_EXPIRES_IN,
|
|
});
|
|
}
|
|
|
|
export function verifyToken(token: string): JWTPayload {
|
|
return jwt.verify(token, env.JWT_SECRET) as JWTPayload;
|
|
}
|
|
|
|
export function decodeToken(token: string): JWTPayload | null {
|
|
try {
|
|
return jwt.decode(token) as JWTPayload;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|