import bcrypt from 'bcryptjs'; import { query } from './db'; export interface Beo { id: number; name: string; vorname: string | null; kürzel: string | null; pw: string | null; MustChangePassword: number; } export async function getBeoByKuerzel(kuerzel: string): Promise { const rows = await query( 'SELECT id, name, vorname, `kürzel`, pw, MustChangePassword FROM beos WHERE `kürzel` = ?', [kuerzel] ) as Beo[]; return rows[0] ?? null; } export async function verifyCredentials( kuerzel: string, password: string ): Promise<{ beo: Beo; valid: boolean } | null> { const beo = await getBeoByKuerzel(kuerzel); if (!beo) return null; if (!beo.pw) { const valid = password === 'logbuch123'; return { beo, valid }; } const valid = await bcrypt.compare(password, beo.pw); return { beo, valid }; } export async function hashPassword(password: string): Promise { return bcrypt.hash(password, 10); } export function getBeoDisplayName(beo: Beo): string { return beo.vorname ? `${beo.vorname} ${beo.name}` : beo.name; }