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.
Files changed (2) hide show
  1. package/lib/models/User.js +73 -66
  2. package/package.json +1 -1
@@ -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
- 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
- urlImg: { type: String },
13
- data: { type: Object },
14
- validateKey: {
15
- limitCodeTime: { type: Number },
16
- resetPassword: {
17
- resetCode: { type: Number },
18
- validCode: { type: Boolean, default: false },
19
- },
20
- validateEmail: {
21
- emailVerified: { type: Boolean, default: false },
22
- verifyMailToken: { type: String },
23
- },
24
- validatePhone: {
25
- codeVerifyPhone: {type: Number },
26
- validCodePhone: {type: Boolean, default: false},
27
- }
28
- },
29
- _functions: [
30
- {
31
- type: ObjectId, required: true, ref: 'Functions'
32
- }
33
- ],
34
- _business: [
35
- {
36
- type: ObjectId, required: false, ref: 'Business'
37
- }
38
- ],
39
- tokens: [
40
- {
41
- token: { type: String, required: true },
42
- date: { type: Number }
43
- }
44
- ],
45
-
46
- status: { type: String },
47
- createdAt: { type: Number },
48
- lastUpdate: { type: Number }
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
- const user = this
59
+ const user = this
53
60
 
54
- if (user.isModified("pwd")) {
55
- user.pwd = await bcrypt.hash(user.pwd, 8)
56
- }
61
+ if (user.isModified("pwd")) {
62
+ user.pwd = await bcrypt.hash(user.pwd, 8)
63
+ }
57
64
 
58
- next()
65
+ next()
59
66
  })
60
67
 
61
68
  adminSchema.methods.generateAuthToken = async function () {
62
- const user = this
69
+ const user = this
63
70
 
64
- const token = jwt.sign({ _id: user._id }, process.env.AUTH_SECRET)
65
- user.tokens = user.tokens.concat({ token })
71
+ const token = jwt.sign({ _id: user._id }, process.env.AUTH_SECRET)
72
+ user.tokens = user.tokens.concat({ token })
66
73
 
67
- await user.save()
74
+ await user.save()
68
75
 
69
- return token
76
+ return token
70
77
  }
71
78
 
72
79
  adminSchema.statics.findByCredentials = async (email, pwd) => {
73
- try {
74
- const user = await User.findOne({ email: email })
75
-
76
- if (!user) {
77
- throw new Error({ error: "Invalid login credentials" })
78
- }
79
-
80
- const isPasswordMatch = await bcrypt.compare(pwd, user.pwd)
81
-
82
- if (!isPasswordMatch) {
83
- throw new Error({ error: "Invalid login credentials" })
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
- return user
87
- } catch (error) {}
93
+ return user
94
+ } catch (error) { }
88
95
  }
89
96
 
90
97
  const User = mongoose.model("User", adminSchema)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aloux-iam",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "description": "Aloux IAM for APIs ",
5
5
  "main": "index.js",
6
6
  "scripts": {