Add password change routes to backend and frontend; update routing and UI components for password management

This commit is contained in:
Torsten Schulz (local)
2025-10-17 23:13:33 +02:00
parent b2cef4d306
commit 4fe6b27b8f
8 changed files with 430 additions and 0 deletions

View 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();