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();
|
||||
|
||||
Reference in New Issue
Block a user