EvalueTonSavoir/server/controllers/users.js

44 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
const emailer = require('../config/email.js');
const jwt = require('../middleware/jwtToken.js');
const AppError = require('../middleware/AppError.js');
2024-04-05 20:10:59 -04:00
const { MISSING_REQUIRED_PARAMETER, LOGIN_CREDENTIALS_ERROR, GENERATE_PASSWORD_ERROR, UPDATE_PASSWORD_ERROR, DELETE_USER_ERROR } = require('../constants/errorCodes');
2024-03-29 20:08:34 -04:00
// controllers must use arrow functions to bind 'this' to the class instance in order to access class properties as callbacks in Express
2024-03-29 20:08:34 -04:00
class UsersController {
constructor(userModel) {
this.users = userModel;
}
2024-03-29 20:08:34 -04:00
async delete(req, res, next) {
try {
const { email, password } = req.body;
2024-03-29 20:08:34 -04:00
if (!email || !password) {
throw new AppError(MISSING_REQUIRED_PARAMETER);
}
2024-03-29 20:08:34 -04:00
// verify creds first
const user = await this.users.login(email, password);
2024-03-29 20:08:34 -04:00
if (!user) {
throw new AppError(LOGIN_CREDENTIALS_ERROR);
}
const result = await this.users.delete(email);
2024-03-29 20:08:34 -04:00
if (!result) {
throw new AppError(DELETE_USER_ERROR);
2024-03-29 20:08:34 -04:00
}
2024-03-29 20:08:34 -04:00
return res.status(200).json({
message: 'Utilisateur supprimé avec succès'
});
} catch (error) {
2024-03-29 20:08:34 -04:00
return next(error);
}
}
}
module.exports = UsersController;