Multi-tenant digital menu platform (Collina Systems): public QR menu with glassmorphism + 3D macro flip cards, mobile-first editor with draft/publish versioning (5-version history + restore), AI consultant & translation (Claude Haiku), realtime orders via SSE, configurable palette, QR generation, superadmin panel, and Docker/Coolify infra.
87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
import { SignJWT, jwtVerify } from 'jose';
|
|
import bcrypt from 'bcryptjs';
|
|
import { cookies } from 'next/headers';
|
|
import crypto from 'crypto';
|
|
|
|
const JWT_SECRET = process.env.JWT_SECRET || 'dev-insecure-secret-change-me';
|
|
const key = new TextEncoder().encode(JWT_SECRET);
|
|
|
|
export type EditorSession = { slug: string; restaurantId: string };
|
|
|
|
export function editorCookieName(slug: string) {
|
|
return `smartmenu_editor_${slug}`;
|
|
}
|
|
|
|
export async function signEditorToken(session: EditorSession): Promise<string> {
|
|
return new SignJWT(session)
|
|
.setProtectedHeader({ alg: 'HS256' })
|
|
.setIssuedAt()
|
|
.setExpirationTime('7d')
|
|
.sign(key);
|
|
}
|
|
|
|
export async function verifyEditorToken(token: string): Promise<EditorSession | null> {
|
|
try {
|
|
const { payload } = await jwtVerify(token, key);
|
|
return { slug: payload.slug as string, restaurantId: payload.restaurantId as string };
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export async function getEditorSession(slug: string): Promise<EditorSession | null> {
|
|
const token = cookies().get(editorCookieName(slug))?.value;
|
|
if (!token) return null;
|
|
const session = await verifyEditorToken(token);
|
|
if (!session || session.slug !== slug) return null;
|
|
return session;
|
|
}
|
|
|
|
export async function hashPassword(password: string): Promise<string> {
|
|
return bcrypt.hash(password, 10);
|
|
}
|
|
|
|
export async function verifyPassword(password: string, hashed: string): Promise<boolean> {
|
|
return bcrypt.compare(password, hashed);
|
|
}
|
|
|
|
export function generateSecret(bytes = 32): string {
|
|
return crypto.randomBytes(bytes).toString('base64url');
|
|
}
|
|
|
|
export function generatePassword(): string {
|
|
return crypto.randomBytes(9).toString('base64url');
|
|
}
|
|
|
|
// Superadmin session (env-based credentials).
|
|
const ADMIN_COOKIE = 'smartmenu_admin';
|
|
|
|
export async function signAdminToken(): Promise<string> {
|
|
return new SignJWT({ role: 'superadmin' })
|
|
.setProtectedHeader({ alg: 'HS256' })
|
|
.setIssuedAt()
|
|
.setExpirationTime('1d')
|
|
.sign(key);
|
|
}
|
|
|
|
export async function isAdminAuthed(): Promise<boolean> {
|
|
const token = cookies().get(ADMIN_COOKIE)?.value;
|
|
if (!token) return false;
|
|
try {
|
|
const { payload } = await jwtVerify(token, key);
|
|
return payload.role === 'superadmin';
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function checkAdminCredentials(user: string, password: string): boolean {
|
|
return (
|
|
!!process.env.SUPERADMIN_USER &&
|
|
user === process.env.SUPERADMIN_USER &&
|
|
password === process.env.SUPERADMIN_PASSWORD
|
|
);
|
|
}
|
|
|
|
export { ADMIN_COOKIE };
|