-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthUserSchema.js
More file actions
134 lines (133 loc) · 4.39 KB
/
AuthUserSchema.js
File metadata and controls
134 lines (133 loc) · 4.39 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
132
133
134
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const crypto = require('crypto')
const moment = require('moment')
const uniqueValidator = require('mongoose-unique-validator');
/**
* AuthUserSchema
* ******************
*
* This define the schema representation
* of the AuthUser model
*/
const AuthUserSchema = function (options = {}) {
// if(options instanceof Object && options !={}){
// def = Object.assign(defaultConfigOptions,options)
// }
const authschem = new mongoose.Schema({
email: {
type: String,
required: options.schemaBooleans.useEmail,
unique: true
},
firstName: {
type: String,
required: options.schemaBooleans.useFirstName
},
otherName: {
type: String,
required: options.schemaBooleans.useOtherName
},
lastName: {
type: String,
required: options.schemaBooleans.useLastName
},
usernameField: {
type: String,
unique: options.schemaBooleans.useUsername,
minlength: options.username.minlength,
maxlength: options.username.maxlength,
required: options.schemaBooleans.useUsername
},
role: {
type: String,
enum: options.roles,
required: options.schemaBooleans.useRoles
},
verified: {
type: Boolean,
default: false
},
accountStatus: {
deleted: {
type: Boolean,
required: true,
default: false,
},
active: {
type: Boolean,
required: true,
default: true,
},
},
deletionMechanism: {
softDeletion: {
type: Boolean,
required: true,
default: options.deletionMechanism.softDeletion
},
legacyDeletion: {
type: Boolean,
required: true,
default: options.deletionMechanism.legacyDeletion
}
},
password: {
type: String,
required: true,
select: false,
minlength: options.password.minlength
},
resetPasswordToken: {
type: String,
required: false
},
resetPasswordTokenExpiryDate: {
type: Date,
required: false
},
confirmationToken: {
type: String,
required: false
},
confirmationTokenExpiryDate: {
type: Date,
required: false
},
profile_photo: {
type: String,
required: options.useProfilePhoto
}
});
authschem.pre('save', async function (next) {
// only hash the password if it has been modified (or is new)
if (!this.isModified('password')) return next();
const salt = await bcrypt.genSalt(11);
const hashedPassword = await bcrypt.hashSync(this.password, salt);
return this.password = hashedPassword;
})
authschem.methods.getJwtSignedToken = function (next) {
return jwt.sign({
_id: this._id
}, options.authSecretKeys.JWT_SECRET_KEY, {
expiresIn: options.authSecretKeys.JWT_EXPIRY_DATE
});
}
authschem.methods.getResetPasswordToken = function (next) {
const resetToken = crypto.randomBytes(32).toString('hex');
this.resetPasswordToken = crypto.createHash('sha256').update(resetToken).digest('hex');
// set expire
this.resetPasswordTokenExpiryDate = moment().add(options.emailCredentials.RESET_PASSWORD_TOKEN_EXPIRY_DATE, options.emailCredentials.RESET_PASSWORD_TOKEN_EXPIRY_DATE_TYPE);
return resetToken;
}
authschem.methods.getEmailConfirmationToken = function (next) {
const token = crypto.randomBytes(32).toString('hex');
this.confirmationToken = crypto.createHash('sha256').update(token).digest('hex');
this.confirmationTokenExpiryDate = moment().add(options.emailCredentials.CONFIRMATION_TOKEN_EXPIRY_DATE, options.emailCredentials.CONFIRMATION_TOKEN_EXPIRY_DATE_TYPE);
return token;
}
authschem.plugin(uniqueValidator);
return authschem;
}
module.exports = AuthUserSchema;