Add password change routes to backend and frontend; update routing and UI components for password management
This commit is contained in:
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