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
|
|
|
|
2024-10-01 18:03:55 -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 {
|
|
|
|
|
|
2024-10-02 10:23:56 -04:00
|
|
|
constructor(userModel) {
|
|
|
|
|
this.users = userModel;
|
2024-10-01 11:30:26 -04:00
|
|
|
}
|
|
|
|
|
|
2024-10-01 18:03:55 -04:00
|
|
|
register = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { email, password } = req.body;
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!email || !password) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
if (!this.users) {
|
|
|
|
|
throw new AppError('Users model not found');
|
|
|
|
|
}
|
|
|
|
|
await this.users.register(email, password);
|
|
|
|
|
|
|
|
|
|
emailer.registerConfirmation(email);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Utilisateur créé avec succès.'
|
|
|
|
|
});
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
login = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { email, password } = req.body;
|
|
|
|
|
|
|
|
|
|
if (!email || !password) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 18:03:55 -04:00
|
|
|
if (!this) {
|
|
|
|
|
throw new AppError('UsersController not initialized');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 token = jwt.create(user.email, user._id);
|
|
|
|
|
|
2025-03-10 18:49:29 -04:00
|
|
|
return res.status(200).json({ token });
|
2024-10-01 18:03:55 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
next(error);
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 18:03:55 -04:00
|
|
|
resetPassword = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { email } = req.body;
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!email) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
const newPassword = await this.users.resetPassword(email);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!newPassword) {
|
|
|
|
|
throw new AppError(GENERATE_PASSWORD_ERROR);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
emailer.newPasswordConfirmation(email, newPassword);
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Nouveau mot de passe envoyé par courriel.'
|
|
|
|
|
});
|
2024-10-01 18:03:55 -04:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
changePassword = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { email, oldPassword, newPassword } = req.body;
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!email || !oldPassword || !newPassword) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// verify creds first
|
2024-10-01 18:03:55 -04:00
|
|
|
const user = await this.users.login(email, oldPassword);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!user) {
|
|
|
|
|
throw new AppError(LOGIN_CREDENTIALS_ERROR);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
const password = await this.users.changePassword(email, newPassword);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!password) {
|
|
|
|
|
throw new AppError(UPDATE_PASSWORD_ERROR);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Mot de passe changé avec succès.'
|
|
|
|
|
});
|
2024-10-01 18:03:55 -04:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
delete = async (req, res, next) => {
|
2024-03-29 20:08:34 -04:00
|
|
|
try {
|
|
|
|
|
const { email, password } = req.body;
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!email || !password) {
|
|
|
|
|
throw new AppError(MISSING_REQUIRED_PARAMETER);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
// verify creds first
|
2024-10-01 18:03:55 -04:00
|
|
|
const user = await this.users.login(email, password);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!user) {
|
|
|
|
|
throw new AppError(LOGIN_CREDENTIALS_ERROR);
|
|
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
|
|
|
|
const result = await this.users.delete(email);
|
|
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
if (!result) {
|
2024-10-01 18:03:55 -04:00
|
|
|
throw new AppError(DELETE_USER_ERROR);
|
2024-03-29 20:08:34 -04:00
|
|
|
}
|
2024-10-01 18:03:55 -04:00
|
|
|
|
2024-03-29 20:08:34 -04:00
|
|
|
return res.status(200).json({
|
|
|
|
|
message: 'Utilisateur supprimé avec succès'
|
|
|
|
|
});
|
2024-10-01 18:03:55 -04:00
|
|
|
} catch (error) {
|
2024-03-29 20:08:34 -04:00
|
|
|
return next(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-01 18:03:55 -04:00
|
|
|
module.exports = UsersController;
|