added debugging for logout at reload

This commit is contained in:
2025-10-09 16:57:26 +02:00
parent 4dcff0f70e
commit 8b4886e5bd
2 changed files with 15 additions and 9 deletions

View File

@@ -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<User>(
'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' });
}