-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.js
36 lines (32 loc) · 877 Bytes
/
user.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
const mongoose = require("mongoose");
let Schema = mongoose.Schema;
const jwt = require("jsonwebtoken");
// require("dotenv").config();
let userSchema = new Schema(
{
first_name: { type: String, default: null },
last_name: { type: String, default: null },
email: { type: String, unique: true },
password: { type: String },
token: { type: String },
},
{ timestamps: true }
);
userSchema.methods.signToken = async function () {
var payload = { userId: this.id, email: this.email };
console.log(payload);
try {
var token = jwt.sign(payload, process.env.TOKEN_KEY);
return token;
} catch (error) {
return error;
}
};
userSchema.methods.userJSON = function (token) {
return {
name: this.first_name + " " + this.last_name,
email: this.email,
token: token,
};
};
module.exports = mongoose.model("User", userSchema);