refactor(auth): update user registration and activation responses for security
- Modified the registerUser and activate functions to return a success status instead of user data, enhancing security by not exposing sensitive information. - Improved error handling in the registration process, including user cleanup on failure and clearer error messages for email-related issues. - Ensured that activation emails are sent without returning user details, maintaining user privacy.
This commit is contained in:
@@ -1,13 +1,11 @@
|
||||
import { register, activateUser, login, logout } from '../services/authService.js';
|
||||
import jwt from 'jsonwebtoken';
|
||||
import UserToken from '../models/UserToken.js';
|
||||
import User from '../models/User.js'; // ggf. Pfad anpassen
|
||||
|
||||
const registerUser = async (req, res, next) => {
|
||||
try {
|
||||
const { email, password } = req.body;
|
||||
const user = await register(email, password);
|
||||
res.status(201).json(user);
|
||||
await register(email, password);
|
||||
// Aus Sicherheitsgründen KEINE Userdaten (Passwort-Hash, Aktivierungscode, ...) zurückgeben
|
||||
res.status(201).json({ success: true });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
@@ -16,8 +14,9 @@ const registerUser = async (req, res, next) => {
|
||||
const activate = async (req, res, next) => {
|
||||
try {
|
||||
const { activationCode } = req.params;
|
||||
const user = await activateUser(activationCode);
|
||||
res.status(200).json(user);
|
||||
await activateUser(activationCode);
|
||||
// Auch bei Aktivierung kein komplettes User-Objekt zurückgeben
|
||||
res.status(200).json({ success: true });
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
|
||||
@@ -5,19 +5,46 @@ import UserToken from '../models/UserToken.js';
|
||||
import { sendActivationEmail } from './emailService.js';
|
||||
|
||||
import { devLog } from '../utils/logger.js';
|
||||
|
||||
const register = async (email, password) => {
|
||||
let createdUser = null;
|
||||
try {
|
||||
const activationCode = Math.random().toString(36).substring(2, 15);
|
||||
const user = await User.create({ email, password, activationCode });
|
||||
createdUser = await User.create({ email, password, activationCode });
|
||||
|
||||
// Aktivierungs‑E-Mail versenden
|
||||
await sendActivationEmail(email, activationCode);
|
||||
return user;
|
||||
|
||||
// Aufrufer bekommt absichtlich KEINE Userdaten zurück (siehe Controller)
|
||||
return;
|
||||
} catch (error) {
|
||||
devLog(error);
|
||||
// 1) Immer Grund im Log mit Kontext ausgeben
|
||||
devLog('[authService.register] Fehler bei Registrierung', error);
|
||||
|
||||
// 2) Falls User bereits angelegt wurde, wieder löschen (Rollback)
|
||||
if (createdUser) {
|
||||
try {
|
||||
await createdUser.destroy();
|
||||
devLog(`[authService.register] Angelegten User (id=${createdUser.id}, email=${createdUser.email}) wegen Fehler wieder gelöscht`);
|
||||
} catch (cleanupError) {
|
||||
devLog('[authService.register] Konnte angelegten User nach Fehler nicht löschen', cleanupError);
|
||||
}
|
||||
}
|
||||
|
||||
// E-Mail existiert bereits
|
||||
if (error.name === 'SequelizeUniqueConstraintError') {
|
||||
const err = new Error('E-Mail-Adresse wird bereits verwendet');
|
||||
err.status = 409;
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Fehler beim Mailversand (nodemailer o.ä.) klarer kommunizieren
|
||||
if (error && (error.code || error.response || error.responseCode)) {
|
||||
const err = new Error('Registrierung fehlgeschlagen: E-Mail-Versand nicht möglich. Bitte Administrator kontaktieren.');
|
||||
err.status = 500;
|
||||
throw err;
|
||||
}
|
||||
|
||||
const err = new Error('Registrierung fehlgeschlagen');
|
||||
err.status = 400;
|
||||
throw err;
|
||||
|
||||
Reference in New Issue
Block a user