Fixed the login again?

This commit is contained in:
2026-06-26 22:30:54 +02:00
parent cd37d5a987
commit 39fb39db12
5 changed files with 51 additions and 58 deletions
+11 -6
View File
@@ -2,14 +2,19 @@ import { SignJWT, jwtVerify } from "jose";
import bcrypt from "bcryptjs";
import { query } from "@/lib/db";
import { cookies } from "next/headers";
import crypto from "crypto";
const RAW_SECRET = process.env.NODE_ENV === "production"
? process.env.JWT_SECRET
: crypto.randomUUID();
if (!RAW_SECRET) {
throw new Error("JWT_SECRET environment variable is required");
function randomHex(length: number): string {
const chars = "abcdef0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars[Math.floor(Math.random() * chars.length)];
}
return result;
}
const g = globalThis as typeof globalThis & { __JWT_RAW_SECRET?: string };
const RAW_SECRET = process.env.JWT_SECRET || (g.__JWT_RAW_SECRET ??= randomHex(32));
if (!RAW_SECRET) throw new Error("JWT_SECRET environment variable is required");
const JWT_SECRET = new TextEncoder().encode(RAW_SECRET);
export const SESSION_COOKIE = "session";