user.js
1.35 KB
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
39
40
41
42
43
44
45
46
47
48
49
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const Schema = mongoose.Schema;
const UserSchema = new Schema ({
userId : { type: String, required : true, unique : true, },
hashedPassword : { type : String, required : true },
userTypeCd : { type : String, required : true, default : 'NORMAL' },
useYn : { type : String, default : 'W', required : true, },
});
UserSchema.methods.setPassword = async function(password) {
const hash = await bcrypt.hash(password, 10);
this.hashedPassword = hash;
};
UserSchema.methods.checkPassword = async function(password) {
const result = await bcrypt.compare(password, this.hashedPassword)
return result;
};
UserSchema.methods.setUseYn = async function(useYn) {
this.useYn = useYn;
}
UserSchema.statics.findByUserId = async function(userId) {
return this.findOne({ userId });
};
UserSchema.statics.findAllByUserTypeCd = async function(userTypeCd) {
return this.find({ userTypeCd });
};
UserSchema.methods.generateToken = function() {
const token = jwt.sign (
{
_id : this._id,
userId : this.userId
},
// eslint-disable-next-line no-undef
process.env.JWT_SECRET,
{ expiresIn : '30d' }
);
return token;
};
module.exports = mongoose.model('User', UserSchema);