aloux-iam 0.0.11 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/models/User.js +73 -66
- package/package.json +1 -1
package/lib/models/User.js
CHANGED
|
@@ -4,87 +4,94 @@ const jwt = require("jsonwebtoken")
|
|
|
4
4
|
const ObjectId = mongoose.Schema.Types.ObjectId
|
|
5
5
|
|
|
6
6
|
const adminSchema = mongoose.Schema({
|
|
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
|
-
|
|
7
|
+
name: { type: String, required: true, trim: true },
|
|
8
|
+
lastName: { type: String, required: false, trim: true },
|
|
9
|
+
email: { type: String, required: true, trim: true, unique: true, lowercase: true },
|
|
10
|
+
pwd: { type: String, trim: true, minLength: 8 },
|
|
11
|
+
phone: { type: String, trim: true, maxLength: 13 },
|
|
12
|
+
phoneObj: {
|
|
13
|
+
phone: { type: String, trim: true, maxLength: 10 },
|
|
14
|
+
phoneInternacional: { type: String, trim: true, maxLength: 13 },
|
|
15
|
+
dialCode: { type: String, trim: true, maxLength: 3 },
|
|
16
|
+
country: { type: String, trim: true, maxLength: 50 },
|
|
17
|
+
regionCode: { type: String, trim: true, maxLength: 3 }
|
|
18
|
+
},
|
|
19
|
+
urlImg: { type: String },
|
|
20
|
+
data: { type: Object },
|
|
21
|
+
validateKey: {
|
|
22
|
+
limitCodeTime: { type: Number },
|
|
23
|
+
resetPassword: {
|
|
24
|
+
resetCode: { type: Number },
|
|
25
|
+
validCode: { type: Boolean, default: false },
|
|
26
|
+
},
|
|
27
|
+
validateEmail: {
|
|
28
|
+
emailVerified: { type: Boolean, default: false },
|
|
29
|
+
verifyMailToken: { type: String },
|
|
30
|
+
},
|
|
31
|
+
validatePhone: {
|
|
32
|
+
codeVerifyPhone: { type: Number },
|
|
33
|
+
validCodePhone: { type: Boolean, default: false },
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
_functions: [
|
|
37
|
+
{
|
|
38
|
+
type: ObjectId, required: true, ref: 'Functions'
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
_business: [
|
|
42
|
+
{
|
|
43
|
+
type: ObjectId, required: false, ref: 'Business'
|
|
44
|
+
}
|
|
45
|
+
],
|
|
46
|
+
tokens: [
|
|
47
|
+
{
|
|
48
|
+
token: { type: String, required: true },
|
|
49
|
+
date: { type: Number }
|
|
50
|
+
}
|
|
51
|
+
],
|
|
52
|
+
|
|
53
|
+
status: { type: String },
|
|
54
|
+
createdAt: { type: Number },
|
|
55
|
+
lastUpdate: { type: Number }
|
|
49
56
|
})
|
|
50
57
|
|
|
51
58
|
adminSchema.pre("save", async function (next) {
|
|
52
|
-
|
|
59
|
+
const user = this
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
61
|
+
if (user.isModified("pwd")) {
|
|
62
|
+
user.pwd = await bcrypt.hash(user.pwd, 8)
|
|
63
|
+
}
|
|
57
64
|
|
|
58
|
-
|
|
65
|
+
next()
|
|
59
66
|
})
|
|
60
67
|
|
|
61
68
|
adminSchema.methods.generateAuthToken = async function () {
|
|
62
|
-
|
|
69
|
+
const user = this
|
|
63
70
|
|
|
64
|
-
|
|
65
|
-
|
|
71
|
+
const token = jwt.sign({ _id: user._id }, process.env.AUTH_SECRET)
|
|
72
|
+
user.tokens = user.tokens.concat({ token })
|
|
66
73
|
|
|
67
|
-
|
|
74
|
+
await user.save()
|
|
68
75
|
|
|
69
|
-
|
|
76
|
+
return token
|
|
70
77
|
}
|
|
71
78
|
|
|
72
79
|
adminSchema.statics.findByCredentials = async (email, pwd) => {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
try {
|
|
81
|
+
const user = await User.findOne({ email: email })
|
|
82
|
+
|
|
83
|
+
if (!user) {
|
|
84
|
+
throw new Error({ error: "Invalid login credentials" })
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const isPasswordMatch = await bcrypt.compare(pwd, user.pwd)
|
|
88
|
+
|
|
89
|
+
if (!isPasswordMatch) {
|
|
90
|
+
throw new Error({ error: "Invalid login credentials" })
|
|
91
|
+
}
|
|
85
92
|
|
|
86
|
-
|
|
87
|
-
|
|
93
|
+
return user
|
|
94
|
+
} catch (error) { }
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
const User = mongoose.model("User", adminSchema)
|