Add password change routes to backend and frontend; update routing and UI components for password management
This commit is contained in:
47
backend/src/controllers/PasswordController.js
Normal file
47
backend/src/controllers/PasswordController.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const PasswordService = require('../services/PasswordService');
|
||||
|
||||
/**
|
||||
* Controller für Passwort-Verwaltung
|
||||
* Verarbeitet HTTP-Requests und delegiert an PasswordService
|
||||
*/
|
||||
class PasswordController {
|
||||
/**
|
||||
* Ändert das Passwort
|
||||
*/
|
||||
async changePassword(req, res) {
|
||||
try {
|
||||
const userId = req.user?.id || 1;
|
||||
const { oldPassword, newPassword, confirmPassword } = req.body;
|
||||
|
||||
if (!oldPassword || !newPassword || !confirmPassword) {
|
||||
return res.status(400).json({
|
||||
message: 'Alle Felder sind erforderlich'
|
||||
});
|
||||
}
|
||||
|
||||
if (newPassword !== confirmPassword) {
|
||||
return res.status(400).json({
|
||||
message: 'Die Passwörter stimmen nicht überein'
|
||||
});
|
||||
}
|
||||
|
||||
await PasswordService.changePassword(userId, oldPassword, newPassword);
|
||||
|
||||
res.json({ message: 'Passwort erfolgreich geändert' });
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Ändern des Passworts:', error);
|
||||
|
||||
// Spezifische Fehlermeldungen
|
||||
if (error.message.includes('alte Passwort')) {
|
||||
return res.status(401).json({ message: error.message });
|
||||
}
|
||||
|
||||
res.status(500).json({
|
||||
message: error.message || 'Fehler beim Ändern des Passworts'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new PasswordController();
|
||||
|
||||
@@ -94,6 +94,10 @@ app.use('/api/holidays', authenticateToken, holidaysRouter);
|
||||
const profileRouter = require('./routes/profile');
|
||||
app.use('/api/profile', authenticateToken, profileRouter);
|
||||
|
||||
// Password routes (geschützt) - MIT ID-Hashing
|
||||
const passwordRouter = require('./routes/password');
|
||||
app.use('/api/password', authenticateToken, passwordRouter);
|
||||
|
||||
// Error handling middleware
|
||||
app.use((err, req, res, next) => {
|
||||
console.error(err.stack);
|
||||
|
||||
13
backend/src/routes/password.js
Normal file
13
backend/src/routes/password.js
Normal file
@@ -0,0 +1,13 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const PasswordController = require('../controllers/PasswordController');
|
||||
|
||||
/**
|
||||
* Routen für Passwort-Verwaltung
|
||||
*/
|
||||
|
||||
// PUT /api/password - Passwort ändern
|
||||
router.put('/', PasswordController.changePassword.bind(PasswordController));
|
||||
|
||||
module.exports = router;
|
||||
|
||||
68
backend/src/services/PasswordService.js
Normal file
68
backend/src/services/PasswordService.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const database = require('../config/database');
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
/**
|
||||
* Service-Klasse für Passwort-Verwaltung
|
||||
*/
|
||||
class PasswordService {
|
||||
/**
|
||||
* Ändert das Passwort eines Benutzers
|
||||
* @param {number} userId - Benutzer-ID
|
||||
* @param {string} oldPassword - Altes Passwort
|
||||
* @param {string} newPassword - Neues Passwort
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async changePassword(userId, oldPassword, newPassword) {
|
||||
const { User, AuthInfo } = database.getModels();
|
||||
|
||||
// Hole User und AuthInfo
|
||||
const user = await User.findByPk(userId, {
|
||||
include: [
|
||||
{
|
||||
model: AuthInfo,
|
||||
as: 'authInfo',
|
||||
required: true
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
if (!user || !user.authInfo) {
|
||||
throw new Error('Benutzer oder Login-Informationen nicht gefunden');
|
||||
}
|
||||
|
||||
const authInfo = user.authInfo;
|
||||
|
||||
// Prüfe ob Passwort gesetzt ist
|
||||
if (!authInfo.password_hash) {
|
||||
throw new Error('Für diesen Account ist kein Passwort gesetzt. Möglicherweise verwenden Sie OAuth-Login.');
|
||||
}
|
||||
|
||||
// Prüfe altes Passwort
|
||||
const isValidPassword = await bcrypt.compare(oldPassword, authInfo.password_hash);
|
||||
|
||||
if (!isValidPassword) {
|
||||
throw new Error('Das alte Passwort ist nicht korrekt');
|
||||
}
|
||||
|
||||
// Prüfe neues Passwort
|
||||
if (!newPassword || newPassword.length < 6) {
|
||||
throw new Error('Das neue Passwort muss mindestens 6 Zeichen lang sein');
|
||||
}
|
||||
|
||||
if (newPassword === oldPassword) {
|
||||
throw new Error('Das neue Passwort darf nicht mit dem alten übereinstimmen');
|
||||
}
|
||||
|
||||
// Hash neues Passwort
|
||||
const hashedPassword = await bcrypt.hash(newPassword, 10);
|
||||
|
||||
// Aktualisiere Passwort
|
||||
authInfo.password_hash = hashedPassword;
|
||||
await authInfo.save();
|
||||
|
||||
console.log(`Passwort für User ${userId} erfolgreich geändert`);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new PasswordService();
|
||||
|
||||
Reference in New Issue
Block a user