EvalueTonSavoir/server/models/users.js

126 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-03-29 20:08:34 -04:00
//user
const bcrypt = require('bcrypt');
const AppError = require('../middleware/AppError.js');
2024-04-05 20:10:59 -04:00
const { USER_ALREADY_EXISTS } = require('../constants/errorCodes');
2024-03-29 20:08:34 -04:00
class Users {
constructor(db, foldersModel) {
2024-10-01 11:30:26 -04:00
console.log("Users constructor: db", db)
this.db = db;
this.folders = foldersModel;
2024-10-01 11:30:26 -04:00
}
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);
}
async verify(password, hash) {
return await bcrypt.compare(password, hash)
}
async register(email, password) {
2024-10-01 11:30:26 -04:00
await this.db.connect()
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection('users');
const existingUser = await userCollection.findOne({ email: email });
if (existingUser) {
throw new AppError(USER_ALREADY_EXISTS);
}
const newUser = {
email: email,
password: await this.hashPassword(password),
created_at: new Date()
};
2024-10-01 11:30:26 -04:00
const result = await userCollection.insertOne(newUser);
console.log("userCollection.insertOne() result", result);
const userId = result.insertedId.toString();
2024-03-29 20:08:34 -04:00
const folderTitle = 'Dossier par Défaut';
await this.folders.create(folderTitle, userId);
2024-03-29 20:08:34 -04:00
2024-10-01 11:30:26 -04:00
return result;
2024-03-29 20:08:34 -04:00
}
async login(email, password) {
2024-10-01 11:30:26 -04:00
await this.db.connect()
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection('users');
const user = await userCollection.findOne({ email: email });
if (!user) {
return false;
}
const passwordMatch = await this.verify(password, user.password);
if (!passwordMatch) {
return false;
}
return user;
}
async resetPassword(email) {
const newPassword = this.generatePassword();
return await this.changePassword(email, newPassword);
}
async changePassword(email, newPassword) {
2024-10-01 11:30:26 -04:00
await this.db.connect()
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection('users');
const hashedPassword = await this.hashPassword(newPassword);
const result = await userCollection.updateOne({ email }, { $set: { password: hashedPassword } });
if (result.modifiedCount != 1) return null;
return newPassword
}
async delete(email) {
2024-10-01 11:30:26 -04:00
await this.db.connect()
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection('users');
const result = await userCollection.deleteOne({ email });
if (result.deletedCount != 1) return false;
return true;
}
async getId(email) {
2024-10-01 11:30:26 -04:00
await this.db.connect()
const conn = this.db.getConnection();
2024-03-29 20:08:34 -04:00
const userCollection = conn.collection('users');
const user = await userCollection.findOne({ email: email });
if (!user) {
return false;
}
return user._id;
}
}
2024-10-01 11:30:26 -04:00
module.exports = Users;