feat: implement JWT authentication system
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>
This commit is contained in:
27
apps/api/src/utils/token.ts
Normal file
27
apps/api/src/utils/token.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user