Co-authored-by: dbroqua <contact@darkou.fr> Reviewed-on: https://git.darkou.fr/dbroqua/MusicTopus/pulls/44
69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
import Joi from "joi";
|
|
|
|
import UsersModel from "../models/users";
|
|
import Pages from "./Pages";
|
|
|
|
/**
|
|
* Classe permettant la gestion de l'utilisateur connecté
|
|
*/
|
|
class Me extends Pages {
|
|
/**
|
|
* Méthode permettant de modifier le profil d'un utilisateur
|
|
* @return {Object}
|
|
*/
|
|
async patchMe() {
|
|
const { body, user } = this.req;
|
|
|
|
const schema = Joi.object({
|
|
isPublicCollection: Joi.boolean(),
|
|
});
|
|
|
|
const value = await schema.validateAsync(body);
|
|
const update = await UsersModel.findByIdAndUpdate(
|
|
user._id,
|
|
{ $set: value },
|
|
{ new: true }
|
|
);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
this.req.login(update, (err) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
return resolve(null);
|
|
});
|
|
});
|
|
|
|
return update;
|
|
}
|
|
|
|
/**
|
|
* Méthode permettant de modifier le mot de passe d'un utilisateur
|
|
*/
|
|
async updatePassword() {
|
|
const { body } = this.req;
|
|
const { _id } = this.req.user;
|
|
|
|
const schema = Joi.object({
|
|
oldPassword: Joi.string().required(),
|
|
password: Joi.string().required(),
|
|
passwordConfirm: Joi.ref("password"),
|
|
});
|
|
|
|
const value = await schema.validateAsync(body);
|
|
const user = await UsersModel.findById(_id);
|
|
|
|
if (!user.validPassword(value.oldPassword)) {
|
|
throw new Error("Votre ancien mot de passe n'est pas valide");
|
|
}
|
|
|
|
user.salt = value.password;
|
|
|
|
await user.save();
|
|
|
|
this.req.flash("success", "Profil correctement mis à jour");
|
|
}
|
|
}
|
|
|
|
export default Me;
|