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>
This commit is contained in:
@@ -3,7 +3,9 @@ import { query } from '@/lib/db';
|
|||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const rows = await query('SELECT ID, Kuerzel, Name FROM beos ORDER BY Name ASC');
|
const rows = await query(
|
||||||
|
'SELECT id AS ID, `kürzel` AS Kuerzel, CONCAT(IFNULL(vorname, \'\'), IF(vorname IS NOT NULL, \' \', \'\'), name) AS Name FROM beos WHERE `kürzel` IS NOT NULL ORDER BY name ASC'
|
||||||
|
) as { ID: number; Kuerzel: string; Name: string }[];
|
||||||
return NextResponse.json(rows);
|
return NextResponse.json(rows);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('GET /api/beos:', error);
|
console.error('GET /api/beos:', error);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export async function changePassword(
|
|||||||
|
|
||||||
const hashed = await hashPassword(newPassword);
|
const hashed = await hashPassword(newPassword);
|
||||||
await query(
|
await query(
|
||||||
'UPDATE beos SET Passwort = ?, MustChangePassword = 0 WHERE ID = ?',
|
'UPDATE beos SET pw = ?, MustChangePassword = 0 WHERE id = ?',
|
||||||
[hashed, session.beoId]
|
[hashed, session.beoId]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
'use server';
|
'use server';
|
||||||
|
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
import { verifyCredentials } from '@/lib/auth';
|
import { verifyCredentials, getBeoDisplayName } from '@/lib/auth';
|
||||||
import { createSession } from '@/lib/session';
|
import { createSession } from '@/lib/session';
|
||||||
|
|
||||||
export async function login(
|
export async function login(
|
||||||
@@ -22,14 +22,14 @@ export async function login(
|
|||||||
}
|
}
|
||||||
|
|
||||||
await createSession({
|
await createSession({
|
||||||
kuerzel: result.beo.Kuerzel,
|
kuerzel: result.beo.kürzel ?? kuerzel,
|
||||||
beoId: result.beo.ID,
|
beoId: result.beo.id,
|
||||||
beoName: result.beo.Name,
|
beoName: getBeoDisplayName(result.beo),
|
||||||
mustChangePassword: result.beo.MustChangePassword === 1,
|
mustChangePassword: result.beo.MustChangePassword === 1,
|
||||||
isAuthenticated: true,
|
isAuthenticated: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (result.beo.MustChangePassword === 1) {
|
if (result.beo.MustChangePassword === 1 || !result.beo.pw) {
|
||||||
redirect('/change-password');
|
redirect('/change-password');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,5 @@
|
|||||||
CREATE TABLE beos (
|
-- Datenbank: sternwarte
|
||||||
ID INT AUTO_INCREMENT PRIMARY KEY,
|
-- Die Tabelle 'beos' ist bereits vorhanden und wird hier nicht neu angelegt.
|
||||||
Kuerzel VARCHAR(10) NOT NULL UNIQUE,
|
|
||||||
Name VARCHAR(100) NOT NULL,
|
|
||||||
Passwort VARCHAR(255),
|
|
||||||
MustChangePassword TINYINT(1) DEFAULT 1,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE objekte (
|
CREATE TABLE objekte (
|
||||||
ID INT AUTO_INCREMENT PRIMARY KEY,
|
ID INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
@@ -27,7 +21,7 @@ CREATE TABLE logbuch (
|
|||||||
WetterDruck DECIMAL(7,1),
|
WetterDruck DECIMAL(7,1),
|
||||||
created_by INT,
|
created_by INT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (created_by) REFERENCES beos(ID)
|
FOREIGN KEY (created_by) REFERENCES beos(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE logbuch_beos (
|
CREATE TABLE logbuch_beos (
|
||||||
@@ -35,7 +29,7 @@ CREATE TABLE logbuch_beos (
|
|||||||
LogbuchID INT NOT NULL,
|
LogbuchID INT NOT NULL,
|
||||||
BeoID INT NOT NULL,
|
BeoID INT NOT NULL,
|
||||||
FOREIGN KEY (LogbuchID) REFERENCES logbuch(ID) ON DELETE CASCADE,
|
FOREIGN KEY (LogbuchID) REFERENCES logbuch(ID) ON DELETE CASCADE,
|
||||||
FOREIGN KEY (BeoID) REFERENCES beos(ID)
|
FOREIGN KEY (BeoID) REFERENCES beos(id)
|
||||||
);
|
);
|
||||||
|
|
||||||
CREATE TABLE logbuch_objekte (
|
CREATE TABLE logbuch_objekte (
|
||||||
|
|||||||
19
lib/auth.ts
19
lib/auth.ts
@@ -2,16 +2,17 @@ import bcrypt from 'bcryptjs';
|
|||||||
import { query } from './db';
|
import { query } from './db';
|
||||||
|
|
||||||
export interface Beo {
|
export interface Beo {
|
||||||
ID: number;
|
id: number;
|
||||||
Kuerzel: string;
|
name: string;
|
||||||
Name: string;
|
vorname: string | null;
|
||||||
Passwort: string | null;
|
kürzel: string | null;
|
||||||
|
pw: string | null;
|
||||||
MustChangePassword: number;
|
MustChangePassword: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getBeoByKuerzel(kuerzel: string): Promise<Beo | null> {
|
export async function getBeoByKuerzel(kuerzel: string): Promise<Beo | null> {
|
||||||
const rows = await query(
|
const rows = await query(
|
||||||
'SELECT ID, Kuerzel, Name, Passwort, MustChangePassword FROM beos WHERE Kuerzel = ?',
|
'SELECT id, name, vorname, `kürzel`, pw, MustChangePassword FROM beos WHERE `kürzel` = ?',
|
||||||
[kuerzel]
|
[kuerzel]
|
||||||
) as Beo[];
|
) as Beo[];
|
||||||
return rows[0] ?? null;
|
return rows[0] ?? null;
|
||||||
@@ -24,15 +25,19 @@ export async function verifyCredentials(
|
|||||||
const beo = await getBeoByKuerzel(kuerzel);
|
const beo = await getBeoByKuerzel(kuerzel);
|
||||||
if (!beo) return null;
|
if (!beo) return null;
|
||||||
|
|
||||||
if (!beo.Passwort) {
|
if (!beo.pw) {
|
||||||
const valid = password === 'logbuch123';
|
const valid = password === 'logbuch123';
|
||||||
return { beo, valid };
|
return { beo, valid };
|
||||||
}
|
}
|
||||||
|
|
||||||
const valid = await bcrypt.compare(password, beo.Passwort);
|
const valid = await bcrypt.compare(password, beo.pw);
|
||||||
return { beo, valid };
|
return { beo, valid };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function hashPassword(password: string): Promise<string> {
|
export async function hashPassword(password: string): Promise<string> {
|
||||||
return bcrypt.hash(password, 10);
|
return bcrypt.hash(password, 10);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getBeoDisplayName(beo: Beo): string {
|
||||||
|
return beo.vorname ? `${beo.vorname} ${beo.name}` : beo.name;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user