mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* chore: cleanup client depend 🧹
* chore: replace joi with zod and remove unused user validator
* chore: move dep from root to api, cleanup other unused api deps
* chore: remove unused dev dep
* chore: update bun lockfile
* fix: bun scripts
* chore: add bun flag to update script
* chore: remove legacy webpack + babel dev deps
* chore: add back dev deps needed for frontend unit testing
* fix(validators): make schemas as expected and more robust with a full test suite of edge cases
* chore: remove axios from root package, remove path from api, update bun
63 lines
1.5 KiB
JavaScript
63 lines
1.5 KiB
JavaScript
const mongoose = require('mongoose');
|
|
const bcrypt = require('bcryptjs');
|
|
const jwt = require('jsonwebtoken');
|
|
const userSchema = require('./schema/userSchema.js');
|
|
const { SESSION_EXPIRY } = process.env ?? {};
|
|
const expires = eval(SESSION_EXPIRY) ?? 1000 * 60 * 15;
|
|
|
|
userSchema.methods.toJSON = function () {
|
|
return {
|
|
id: this._id,
|
|
provider: this.provider,
|
|
email: this.email,
|
|
name: this.name,
|
|
username: this.username,
|
|
avatar: this.avatar,
|
|
role: this.role,
|
|
emailVerified: this.emailVerified,
|
|
plugins: this.plugins,
|
|
createdAt: this.createdAt,
|
|
updatedAt: this.updatedAt,
|
|
};
|
|
};
|
|
|
|
userSchema.methods.generateToken = function () {
|
|
const token = jwt.sign(
|
|
{
|
|
id: this._id,
|
|
username: this.username,
|
|
provider: this.provider,
|
|
email: this.email,
|
|
},
|
|
process.env.JWT_SECRET,
|
|
{ expiresIn: expires / 1000 },
|
|
);
|
|
return token;
|
|
};
|
|
|
|
userSchema.methods.comparePassword = function (candidatePassword, callback) {
|
|
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
|
|
if (err) {
|
|
return callback(err);
|
|
}
|
|
callback(null, isMatch);
|
|
});
|
|
};
|
|
|
|
module.exports.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;
|
|
};
|
|
|
|
const User = mongoose.model('User', userSchema);
|
|
|
|
module.exports = User;
|