user.js
954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const mongoose = require('mongoose');
const bycrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const Schema = mongoose.Schema;
const UserSchema = new Schema({
userId : { type: String, require : true },
hashedPassword : { type : String, default : null }
})
UserSchema.methods.setPassword = async(password) => {
const hash = await bycrypt(password, 10);
this.hashedPassword = hash;
}
UserSchema.methods.checkPassword = async(password) => {
const result = await bycrypt.compare(password, this.hashedPassword)
return result;
}
UserSchema.statics.findByUserId = async(userId) => {
return this.findOne({userId});
}
UserSchema.methods.generateToken = () => {
const token = jwt.sign (
{
_id : this._id,
userId : this.userId
},
process.env.JWT_SECRET,
{ expiresIn : '30d' }
);
return token;
}
module.exports = mongoose.model("User", UserSchema);