Some checks failed
🚀 Continuous Integration / 🔧 Backend Tests (18.x) (push) Has been cancelled
🚀 Continuous Integration / 🔧 Backend Tests (20.x) (push) Has been cancelled
🚀 Continuous Integration / 🎨 Frontend Tests (18.x) (push) Has been cancelled
🚀 Continuous Integration / 🎨 Frontend Tests (20.x) (push) Has been cancelled
🚀 Continuous Integration / 🔍 Code Quality (push) Has been cancelled
🚀 Continuous Integration / 🔒 Security Checks (push) Has been cancelled
🚀 Continuous Integration / 🎨 Theme Tests (push) Has been cancelled
🚀 Continuous Integration / ♿ Accessibility Tests (push) Has been cancelled
🚀 Continuous Integration / 📱 Cross-Browser Tests (push) Has been cancelled
🚀 Continuous Integration / 🏗️ Build Tests (push) Has been cancelled
🚀 Continuous Integration / 📊 Performance Tests (push) Has been cancelled
🚀 Continuous Integration / 🎯 Integration Tests (push) Has been cancelled
🚀 Continuous Integration / ✅ All Tests Passed (push) Has been cancelled
85 lines
2.1 KiB
JavaScript
85 lines
2.1 KiB
JavaScript
const jwt = require('jsonwebtoken');
|
|
const Child = require('../models/Child');
|
|
|
|
// Middleware für Kinder-Authentifizierung
|
|
const childAuth = async (req, res, next) => {
|
|
// Token aus Header oder Body holen
|
|
const token = req.header('x-child-auth-token') ||
|
|
req.header('x-auth-token') ||
|
|
req.body.token;
|
|
|
|
// Prüfen ob Token vorhanden
|
|
if (!token) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
message: 'Kein Zugangs-Token, Zugriff verweigert'
|
|
});
|
|
}
|
|
|
|
try {
|
|
// Token verifizieren
|
|
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
|
|
|
// Prüfen ob es ein Kind-Token ist
|
|
if (!decoded.child) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
message: 'Ungültiger Token-Typ'
|
|
});
|
|
}
|
|
|
|
// Kind aus Datenbank laden
|
|
const child = await Child.findById(decoded.child.id);
|
|
|
|
if (!child || !child.isActive) {
|
|
return res.status(401).json({
|
|
success: false,
|
|
message: 'Kind nicht gefunden oder deaktiviert'
|
|
});
|
|
}
|
|
|
|
// Kind-Daten zu Request hinzufügen
|
|
req.child = child;
|
|
req.childId = child._id;
|
|
req.parentId = child.parent;
|
|
|
|
next();
|
|
} catch (error) {
|
|
console.error('Kinder-Auth-Middleware-Fehler:', error.message);
|
|
res.status(401).json({
|
|
success: false,
|
|
message: 'Token ist nicht gültig'
|
|
});
|
|
}
|
|
};
|
|
|
|
// Middleware für optionale Kinder-Authentifizierung
|
|
const optionalChildAuth = async (req, res, next) => {
|
|
const token = req.header('x-child-auth-token') ||
|
|
req.header('x-auth-token') ||
|
|
req.body.token;
|
|
|
|
if (!token) {
|
|
return next();
|
|
}
|
|
|
|
try {
|
|
const decoded = jwt.verify(token, process.env.JWT_SECRET);
|
|
|
|
if (decoded.child) {
|
|
const child = await Child.findById(decoded.child.id);
|
|
if (child && child.isActive) {
|
|
req.child = child;
|
|
req.childId = child._id;
|
|
req.parentId = child.parent;
|
|
}
|
|
}
|
|
} catch (error) {
|
|
// Bei optionaler Auth ignorieren wir Fehler
|
|
console.log('Optionale Kinder-Auth fehlgeschlagen:', error.message);
|
|
}
|
|
|
|
next();
|
|
};
|
|
|
|
module.exports = { childAuth, optionalChildAuth }; |