1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const bcrypt = require("bcrypt-nodejs");
|
4 | const crypto = require("crypto");
|
5 | const mongoose = require("mongoose");
|
6 | const 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 |
|
28 |
|
29 | userSchema.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 | });
|
47 | userSchema.methods.comparePassword = function (candidatePassword, cb) {
|
48 | bcrypt.compare(candidatePassword, this.Password, (err, isMatch) => {
|
49 | cb(err, isMatch);
|
50 | });
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 | userSchema.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 |
|
66 | const User = mongoose.model("User", userSchema);
|
67 | exports.default = User;
|
68 |
|
\ | No newline at end of file |