Compare commits

...

2 Commits

2 changed files with 1046 additions and 1143 deletions
+1026 -1123
View File
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -3,11 +3,11 @@ import { SignJWT, jwtVerify } from 'jose';
import bcrypt from 'bcryptjs'; import bcrypt from 'bcryptjs';
import { db } from './db'; import { db } from './db';
const JWT_SECRET = process.env.JWT_SECRET; function getSecret() {
if (!JWT_SECRET) { const s = process.env.JWT_SECRET;
throw new Error('JWT_SECRET is not configured'); if (!s) throw new Error('JWT_SECRET is not configured');
return new TextEncoder().encode(s);
} }
const secret = new TextEncoder().encode(JWT_SECRET);
export async function hashPassword(password: string) { export async function hashPassword(password: string) {
return bcrypt.hashSync(password, 10); return bcrypt.hashSync(password, 10);
@@ -39,7 +39,7 @@ export async function createSession(user: { id: number; username: string; role:
const token = await new SignJWT({ id: user.id, username: user.username, role: user.role }) const token = await new SignJWT({ id: user.id, username: user.username, role: user.role })
.setProtectedHeader({ alg: 'HS256' }) .setProtectedHeader({ alg: 'HS256' })
.setExpirationTime('7d') .setExpirationTime('7d')
.sign(secret); .sign(getSecret());
cookies().set('session', token, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'lax', maxAge: 60 * 60 * 24 * 7 }); cookies().set('session', token, { httpOnly: true, secure: process.env.NODE_ENV === 'production', sameSite: 'lax', maxAge: 60 * 60 * 24 * 7 });
return token; return token;
} }
@@ -48,7 +48,7 @@ export async function getSession() {
const token = cookies().get('session')?.value; const token = cookies().get('session')?.value;
if (!token) return null; if (!token) return null;
try { try {
const { payload } = await jwtVerify(token, secret); const { payload } = await jwtVerify(token, getSecret());
return payload as { id: number; username: string; role: string }; return payload as { id: number; username: string; role: string };
} catch { } catch {
return null; return null;