-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathuser.js
More file actions
131 lines (125 loc) · 3.27 KB
/
Copy pathuser.js
File metadata and controls
131 lines (125 loc) · 3.27 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/***********************************************************
* Loading all required dependencies, libraries and packages
**********************************************************/
const Sequelize = require("sequelize");
const { sequelize } = require("../../../connection");
const bcrypt = require("bcryptjs");
// setup User model and its fields.
var User = sequelize.define(
"users",
{
username: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
email: {
type: Sequelize.STRING,
unique: true,
allowNull: true,
validate: {
isEmail: true,
isUnique: function (value, next) {
var self = this;
User.findOne({ where: { email: value } })
.then(function (user) {
// reject if a different user wants to use the same email
if (value != null && value != "" && user && self.id !== user.id) {
return next("User email already exists!");
}
return next();
})
.catch(function (err) {
return next(err);
});
},
},
},
password: {
type: Sequelize.STRING,
allowNull: false,
},
permission: {
type: Sequelize.ENUM,
values: ["000", "001", "010", "011", "100", "101", "110", "111"],
allowNull: false,
defaultValue: "000",
},
token: {
type: Sequelize.DataTypes.STRING(2048),
allowNull: true,
},
reset_token: {
type: Sequelize.DataTypes.STRING(2048),
allowNull: true,
},
reset_token_expiration: {
type: Sequelize.DataTypes.BIGINT,
allowNull: true,
},
},
{
hooks: {
beforeCreate: (user) => {
const salt = bcrypt.genSaltSync();
user.password = bcrypt.hashSync(user.password, salt);
},
beforeUpdate: (user) => {
const salt = bcrypt.genSaltSync();
user.password = bcrypt.hashSync(user.password, salt);
},
},
},
{
timestamps: true,
}
);
// Instance Method for validating user's password
User.prototype.validPassword = function (password, user) {
return bcrypt.compareSync(password, user.password);
};
// Adds to the table, never removes
const up = async () => {
// resetToken column
await sequelize
.query(
`ALTER TABLE users ADD COLUMN IF NOT EXISTS reset_token varchar(2048) NULL;`
)
.then(() => {
return null;
})
.catch((err) => {
logger(
"error",
`Failed to add users.reset_token column. DB tables may be out of sync!`,
"user",
null,
err
);
return null;
});
// resetTokenExpiration column
await sequelize
.query(
`ALTER TABLE users ADD COLUMN IF NOT EXISTS reset_token_expiration BIGINT NULL;`
)
.then(() => {
return null;
})
.catch((err) => {
logger(
"error",
`Failed to add users.reset_token_expiration column. DB tables may be out of sync!`,
"user",
null,
err
);
return null;
});
};
// export User model for use in other files.
module.exports = User;
module.exports = {
User: User,
up,
};