Files
logbuch/lib/auth.ts
Reinhard X. Fürst 12be2f1db2 Adapt to existing beos table in database sternwarte
Column mapping: id, name/vorname, kürzel (with umlaut), pw instead of
the planned schema. DB_NAME changed to sternwarte. create_table.sql
no longer creates beos, only the three new logbuch tables.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-27 17:34:45 +02:00

44 lines
1.1 KiB
TypeScript

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<Beo | null> {
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<string> {
return bcrypt.hash(password, 10);
}
export function getBeoDisplayName(beo: Beo): string {
return beo.vorname ? `${beo.vorname} ${beo.name}` : beo.name;
}