mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-11-30 22:45:46 +01:00
fixed login
This commit is contained in:
@@ -1,18 +1,24 @@
|
||||
// backend/src/controllers/setupController.ts
|
||||
import { Request, Response } from 'express';
|
||||
import bcrypt from 'bcrypt';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { db } from '../services/databaseService.js';
|
||||
|
||||
export const checkSetupStatus = async (req: Request, res: Response): Promise<void> => {
|
||||
try {
|
||||
const adminExists = await db.get<{ 'COUNT(*)': number }>(
|
||||
'SELECT COUNT(*) FROM users WHERE role = ?',
|
||||
'SELECT COUNT(*) FROM users WHERE role = ? AND is_active = 1',
|
||||
['admin']
|
||||
);
|
||||
|
||||
console.log('Admin exists check:', adminExists);
|
||||
|
||||
// Korrekte Rückgabe - needsSetup sollte true sein wenn KEIN Admin existiert
|
||||
const needsSetup = !adminExists || adminExists['COUNT(*)'] === 0;
|
||||
|
||||
res.json({
|
||||
needsSetup: !adminExists || adminExists['COUNT(*)'] === 0
|
||||
needsSetup: needsSetup
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error checking setup status:', error);
|
||||
@@ -26,11 +32,14 @@ export const setupAdmin = async (req: Request, res: Response): Promise<void> =>
|
||||
try {
|
||||
// Check if admin already exists
|
||||
const adminExists = await db.get<{ 'COUNT(*)': number }>(
|
||||
'SELECT COUNT(*) FROM users WHERE role = ?',
|
||||
'SELECT COUNT(*) FROM users WHERE role = ? AND is_active = 1',
|
||||
['admin']
|
||||
);
|
||||
|
||||
console.log('🔍 Admin exists check:', adminExists);
|
||||
|
||||
if (adminExists && adminExists['COUNT(*)'] > 0) {
|
||||
console.log('❌ Admin already exists');
|
||||
res.status(400).json({ error: 'Admin existiert bereits' });
|
||||
return;
|
||||
}
|
||||
@@ -38,6 +47,8 @@ export const setupAdmin = async (req: Request, res: Response): Promise<void> =>
|
||||
const { password, name, phone, department } = req.body;
|
||||
const email = 'admin@instandhaltung.de'; // Fixed admin email
|
||||
|
||||
console.log('👤 Creating admin with data:', { name, email, phone, department });
|
||||
|
||||
// Validation
|
||||
if (!password || !name) {
|
||||
res.status(400).json({ error: 'Passwort und Name sind erforderlich' });
|
||||
@@ -52,29 +63,33 @@ export const setupAdmin = async (req: Request, res: Response): Promise<void> =>
|
||||
|
||||
// Hash password
|
||||
const hashedPassword = await bcrypt.hash(password, 10);
|
||||
const adminId = randomUUID();
|
||||
const adminId = uuidv4();
|
||||
|
||||
console.log('📝 Inserting admin user with ID:', adminId);
|
||||
|
||||
try {
|
||||
// Create admin user
|
||||
await db.run(
|
||||
`INSERT INTO users (id, email, password, name, role, phone, department, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||
[adminId, email, hashedPassword, name, 'admin', phone || null, department || null, true]
|
||||
[adminId, email, hashedPassword, name, 'admin', phone || null, department || null, 1]
|
||||
);
|
||||
|
||||
console.log('✅ Admin user created successfully');
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
message: 'Admin erfolgreich erstellt',
|
||||
email: email
|
||||
});
|
||||
} catch (dbError) {
|
||||
console.error('Database error during admin creation:', dbError);
|
||||
console.error('❌ Database error during admin creation:', dbError);
|
||||
res.status(500).json({
|
||||
error: 'Fehler beim Erstellen des Admin-Accounts'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error in setup:', error);
|
||||
console.error('❌ Error in setup:', error);
|
||||
res.status(500).json({
|
||||
error: 'Ein unerwarteter Fehler ist aufgetreten'
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user