From 8b4886e5bd61f65c03024f1cbad12762cd8a1db1 Mon Sep 17 00:00:00 2001 From: donpat1to Date: Thu, 9 Oct 2025 16:57:26 +0200 Subject: [PATCH] added debugging for logout at reload --- backend/src/controllers/authController.ts | 21 +++++++++++++-------- backend/src/middleware/auth.ts | 3 ++- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/backend/src/controllers/authController.ts b/backend/src/controllers/authController.ts index b0529dd..d9575a4 100644 --- a/backend/src/controllers/authController.ts +++ b/backend/src/controllers/authController.ts @@ -2,6 +2,7 @@ import { Request, Response } from 'express'; import jwt from 'jsonwebtoken'; import bcrypt from 'bcrypt'; import { db } from '../services/databaseService.js'; +import { AuthRequest } from '../middleware/auth.js'; export interface User { id: number; @@ -71,13 +72,15 @@ export const login = async (req: Request, res: Response) => { return res.status(401).json({ error: 'UngΓΌltige Anmeldedaten' }); } - // Create token payload - ID als STRING verwenden - const tokenPayload: JWTPayload = { - id: user.id.toString(), // ← WICHTIG: Als string + // Create token payload - KORREKT: id field verwenden + const tokenPayload = { + id: user.id.toString(), // ← WICHTIG: Dies wird als 'id' im JWT gespeichert email: user.email, role: user.role }; + console.log('🎫 Creating JWT with payload:', tokenPayload); + // Create token const token = jwt.sign( tokenPayload, @@ -102,23 +105,25 @@ export const login = async (req: Request, res: Response) => { export const getCurrentUser = async (req: Request, res: Response) => { try { - const jwtUser = (req as any).user as JWTPayload; - console.log('πŸ” Getting current user for ID:', jwtUser?.id); + const authReq = req as AuthRequest; + const jwtUser = authReq.user; - if (!jwtUser?.id) { + console.log('πŸ” Getting current user for ID:', jwtUser?.userId); + + if (!jwtUser?.userId) { console.log('❌ No user ID in JWT'); return res.status(401).json({ error: 'Nicht authentifiziert' }); } const user = await db.get( 'SELECT id, email, name, role, phone, department FROM users WHERE id = ? AND is_active = 1', - [jwtUser.id] + [jwtUser.userId] // ← HIER: userId verwenden ); console.log('πŸ” User found in database:', user ? 'Yes' : 'No'); if (!user) { - console.log('❌ User not found in database for ID:', jwtUser.id); + console.log('❌ User not found in database for ID:', jwtUser.userId); return res.status(404).json({ error: 'Benutzer nicht gefunden' }); } diff --git a/backend/src/middleware/auth.ts b/backend/src/middleware/auth.ts index ef7e987..a573513 100644 --- a/backend/src/middleware/auth.ts +++ b/backend/src/middleware/auth.ts @@ -26,8 +26,9 @@ export const authMiddleware = (req: AuthRequest, res: Response, next: NextFuncti try { const decoded = jwt.verify(token, JWT_SECRET) as any; - console.log('βœ… Token valid for user:', decoded.email); + console.log('βœ… Token valid for user:', decoded.email, 'ID:', decoded.id); + // KORREKTUR: Verwende 'id' aus dem JWT Payload req.user = { userId: decoded.id, email: decoded.email,