swadhinbiswas feat: optimize Docker CI/CD builds, refine site-wide UI aesthetic, and remediate secrets · 8/19/2026
c3e5c18
25 lines · 1.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { SignJWT, jwtVerify } from "jose";
import bcrypt from "bcryptjs";
function getSecret(): Uint8Array {
  const secretStr = (
    process.env.JWT_SECRET ||
    (typeof import.meta !== "undefined" && (import.meta as any).env?.JWT_SECRET) ||
    "community-dev-secret-change-me"
  ).trim();
  return new TextEncoder().encode(secretStr);
}

export async function hashPassword(pw: string) { return bcrypt.hash(pw, 10); }
export async function verifyPassword(pw: string, hash: string) { return bcrypt.compare(pw, hash); }
export async function signToken(payload: Record<string, any>) {
  return await new SignJWT(payload).setProtectedHeader({ alg: "HS256" }).setIssuedAt().setExpirationTime("7d").sign(getSecret());
}
export async function verifyToken(token: string) {
  try { const { payload } = await jwtVerify(token, getSecret()); return payload as any; } catch { return null; }
}
export function getUserFromCookie(cookieHeader: string | null) {
  if (!cookieHeader) return null;
  const m = cookieHeader.match(/community_session=([^;]+)/);
  return m ? m[1] : null;
}