mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00
47 lines
1.3 KiB
JavaScript
47 lines
1.3 KiB
JavaScript
![]() |
const bcrypt = require('bcryptjs');
|
||
|
const User = require('./User');
|
||
|
|
||
|
const hashPassword = async (password) => {
|
||
|
const hashedPassword = await new Promise((resolve, reject) => {
|
||
|
bcrypt.hash(password, 10, function (err, hash) {
|
||
|
if (err) {
|
||
|
reject(err);
|
||
|
} else {
|
||
|
resolve(hash);
|
||
|
}
|
||
|
});
|
||
|
});
|
||
|
|
||
|
return hashedPassword;
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Retrieve a user by ID and convert the found user document to a plain object.
|
||
|
*
|
||
|
* @param {string} userId - The ID of the user to find and return as a plain object.
|
||
|
* @returns {Promise<Object>} A plain object representing the user document, or `null` if no user is found.
|
||
|
*/
|
||
|
const getUser = async function (userId) {
|
||
|
return await User.findById(userId).lean();
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Update a user with new data without overwriting existing properties.
|
||
|
*
|
||
|
* @param {string} userId - The ID of the user to update.
|
||
|
* @param {Object} updateData - An object containing the properties to update.
|
||
|
* @returns {Promise<Object>} The updated user document as a plain object, or `null` if no user is found.
|
||
|
*/
|
||
|
const updateUser = async function (userId, updateData) {
|
||
|
return await User.findByIdAndUpdate(userId, updateData, {
|
||
|
new: true,
|
||
|
runValidators: true,
|
||
|
}).lean();
|
||
|
};
|
||
|
|
||
|
module.exports = {
|
||
|
hashPassword,
|
||
|
updateUser,
|
||
|
getUser,
|
||
|
};
|