mirror of
https://github.com/donpat1to/Schichtenplaner.git
synced 2025-12-01 06:55:45 +01:00
added debugging for logout at reload
This commit is contained in:
@@ -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' });
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user