UNPKG

2.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const bcrypt = require("bcrypt-nodejs");
4const crypto = require("crypto");
5const mongoose = require("mongoose");
6const userSchema = new mongoose.Schema({
7 Username: { type: String, unique: true },
8 Password: String,
9 PasswordResetToken: String,
10 PasswordResetExpires: Date,
11 Roles: { type: Array, default: [] },
12 Groups: { type: Array, default: [] },
13 facebook: String,
14 twitter: String,
15 google: String,
16 tokens: Array,
17 profile: {
18 Name: String,
19 Email: String,
20 Gender: String,
21 Location: String,
22 Website: String,
23 Picture: String
24 }
25}, { timestamps: true });
26/**
27 * Password hash middleware.
28 */
29userSchema.pre("save", function save(next) {
30 const user = this;
31 if (!user.isModified("Password")) {
32 return next();
33 }
34 bcrypt.genSalt(10, (err, salt) => {
35 if (err) {
36 return next(err);
37 }
38 bcrypt.hash(user.Password, salt, undefined, (err, hash) => {
39 if (err) {
40 return next(err);
41 }
42 user.Password = hash;
43 next();
44 });
45 });
46});
47userSchema.methods.comparePassword = function (candidatePassword, cb) {
48 bcrypt.compare(candidatePassword, this.Password, (err, isMatch) => {
49 cb(err, isMatch);
50 });
51};
52/**
53 * Helper method for getting user's gravatar.
54 */
55userSchema.methods.gravatar = function (size) {
56 if (!size) {
57 size = 200;
58 }
59 if (!this.Email) {
60 return `https://gravatar.com/avatar/?s=${size}&d=retro`;
61 }
62 const md5 = crypto.createHash("md5").update(this.Email).digest("hex");
63 return `https://gravatar.com/avatar/${md5}?s=${size}&d=retro`;
64};
65// export const User: UserType = mongoose.model<UserType>('User', userSchema);
66const User = mongoose.model("User", userSchema);
67exports.default = User;
68//# sourceMappingURL=user.model.js.map
\No newline at end of file