deno.land / x / mongoose@6.7.5 / examples / redis-todo / db / models / userModel.js

نووسراو ببینە
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
50
'use strict';
const mongoose = require('mongoose');const jwt = require('jsonwebtoken');const bcrypt = require('bcryptjs');const JWT_SECRET = require('../../config').JWT_SECRET;
const { Schema, model } = mongoose;
const userSchema = new Schema({ name: { type: String, required: true }, username: { type: String, unique: true, required: true }, email: { type: String, unique: true, required: true }, passwordId: { type: mongoose.Types.ObjectId, ref: 'Password' }}, { timestamps: true, versionKey: false });
const userPasswordSchema = new Schema({ password: { type: String, required: true }});
userSchema.methods.toJSON = function() { const user = this.toObject(); // this = user delete user.password; delete user.email; return user;};
// creating tokenuserSchema.methods.genAuthToken = function() { return jwt.sign({ userId: this._id.toString() }, JWT_SECRET); // this = user};
// password hasinguserPasswordSchema.pre('save', async function(next) { try { if (this.isModified('password')) { this.password = await bcrypt.hashSync(this.password, 8); return next(); } next(); } catch (err) { return next(err); }});
module.exports = { User: model('User', userSchema), Password: model('Password', userPasswordSchema)};
mongoose

Version Info

Tagged at
2 years ago