EvalueTonSavoir/server/models/users.js

181 lines
4 KiB
JavaScript
Raw Normal View History

const bcrypt = require("bcrypt");
const AppError = require("../middleware/AppError.js");
const { USER_ALREADY_EXISTS } = require("../constants/errorCodes");
2024-03-29 20:08:34 -04:00
class Users {
constructor(db, foldersModel) {
this.db = db;
this.folders = foldersModel;
}
2024-03-29 20:08:34 -04:00
async hashPassword(password) {
return await bcrypt.hash(password, 10);
}
generatePassword() {
return Math.random().toString(36).slice(-8);
}
2024-03-29 20:08:34 -04:00
async verify(password, hash) {
return await bcrypt.compare(password, hash);
}
async register(userInfos) {
await this.db.connect();
const conn = this.db.getConnection();
const userCollection = conn.collection("users");
const existingUser = await userCollection.findOne({ email: userInfos.email });
if (existingUser) {
throw new AppError(USER_ALREADY_EXISTS);
2024-03-29 20:08:34 -04:00
}
let newUser = {
name: userInfos.name ?? userInfos.email,
email: userInfos.email,
password: await this.hashPassword(userInfos.password),
created_at: new Date(),
roles: userInfos.roles
};
2024-03-29 20:08:34 -04:00
let created_user = await userCollection.insertOne(newUser);
let user = await this.getById(created_user.insertedId)
2024-03-29 20:08:34 -04:00
const folderTitle = "Dossier par Défaut";
const userId = newUser._id ? newUser._id.toString() : 'x';
await this.folders.create(folderTitle, userId);
2024-03-29 20:08:34 -04:00
// TODO: verif if inserted properly...
return user;
}
2024-03-29 20:08:34 -04:00
async login(email, password) {
try {
await this.db.connect();
const conn = this.db.getConnection();
const userCollection = conn.collection("users");
const user = await userCollection.findOne({ email: email });
if (!user) {
const error = new Error("User not found");
error.statusCode = 404;
throw error;
}
const passwordMatch = await this.verify(password, user.password);
if (!passwordMatch) {
const error = new Error("Password does not match");
error.statusCode = 401;
throw error;
}
return user;
} catch (error) {
console.error(error);
throw error;
}
}
2025-03-01 23:01:31 -05:00
async resetPassword(email) {
const newPassword = this.generatePassword();
return await this.changePassword(email, newPassword);
}
2024-03-29 20:08:34 -04:00
async changePassword(email, newPassword) {
await this.db.connect();
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection("users");
2024-03-29 20:08:34 -04:00
const hashedPassword = await this.hashPassword(newPassword);
2024-03-29 20:08:34 -04:00
const result = await userCollection.updateOne(
{ email },
{ $set: { password: hashedPassword } }
);
2024-03-29 20:08:34 -04:00
if (result.modifiedCount != 1) return null;
2024-03-29 20:08:34 -04:00
return newPassword;
}
async delete(email) {
await this.db.connect();
const conn = this.db.getConnection();
const userCollection = conn.collection("users");
const result = await userCollection.deleteOne({ email });
2024-03-29 20:08:34 -04:00
if (result.deletedCount != 1) return false;
return true;
}
2024-03-29 20:08:34 -04:00
async getId(email) {
await this.db.connect();
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection("users");
2024-03-29 20:08:34 -04:00
const user = await userCollection.findOne({ email: email });
2024-03-29 20:08:34 -04:00
if (!user) {
return false;
2024-03-29 20:08:34 -04:00
}
return user._id;
}
async getById(id) {
await this.db.connect();
const conn = this.db.getConnection();
const userCollection = conn.collection("users");
2024-03-29 20:08:34 -04:00
const user = await userCollection.findOne({ _id: id });
if (!user) {
return false;
}
2024-03-29 20:08:34 -04:00
return user;
}
async editUser(userInfo) {
await this.db.connect();
const conn = this.db.getConnection();
const userCollection = conn.collection("users");
const user = await userCollection.findOne({ _id: userInfo.id });
if (!user) {
return false;
}
const updatedFields = { ...userInfo };
delete updatedFields.id;
2024-03-29 20:08:34 -04:00
const result = await userCollection.updateOne(
{ _id: userInfo.id },
{ $set: updatedFields }
);
2024-03-29 20:08:34 -04:00
if (result.modifiedCount === 1) {
return true;
2024-03-29 20:08:34 -04:00
}
return false;
}
2024-03-29 20:08:34 -04:00
}
2024-10-01 11:30:26 -04:00
module.exports = Users;