@igea/oac_backend 1.0.25 → 1.0.26
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/package.json +2 -1
- package/src/config/development.json +10 -1
- package/src/config/production.json +10 -1
- package/src/controllers/users.js +18 -1
- package/src/models/users.js +44 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@igea/oac_backend",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.26",
|
|
4
4
|
"description": "Backend service for the OAC project",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
"@igea/oac_jwt_helpers": "1.0.10",
|
|
23
23
|
"axios": "1.10.0",
|
|
24
24
|
"cookie-parser": "1.4.7",
|
|
25
|
+
"crypto": "1.0.1",
|
|
25
26
|
"express": "5.1.0",
|
|
26
27
|
"get-port": "7.1.0",
|
|
27
28
|
"knex": "3.1.0",
|
|
@@ -21,5 +21,14 @@
|
|
|
21
21
|
"prefix": "diagnostica"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"jwt_secret": "@igea#"
|
|
24
|
+
"jwt_secret": "@igea#",
|
|
25
|
+
"smtp": {
|
|
26
|
+
"host": "smtp.aruba.it",
|
|
27
|
+
"port": 465,
|
|
28
|
+
"secure": true,
|
|
29
|
+
"auth": {
|
|
30
|
+
"user": "your_smtp_user",
|
|
31
|
+
"pass": "your_smtp_password"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
25
34
|
}
|
|
@@ -21,5 +21,14 @@
|
|
|
21
21
|
"prefix": "diagnostica"
|
|
22
22
|
}
|
|
23
23
|
},
|
|
24
|
-
"jwt_secret": "@igea#"
|
|
24
|
+
"jwt_secret": "@igea#",
|
|
25
|
+
"smtp": {
|
|
26
|
+
"host": "smtp.aruba.it",
|
|
27
|
+
"port": 465,
|
|
28
|
+
"secure": true,
|
|
29
|
+
"auth": {
|
|
30
|
+
"user": "your_smtp_user",
|
|
31
|
+
"pass": "your_smtp_password"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
25
34
|
}
|
package/src/controllers/users.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const express = require('express');
|
|
2
2
|
const router = express.Router();
|
|
3
3
|
const Users = require('../models/users');
|
|
4
|
-
|
|
4
|
+
const { randomUUID } = require('crypto');
|
|
5
5
|
|
|
6
6
|
// Get all users
|
|
7
7
|
router.get('/', async (req, res) => {
|
|
@@ -118,4 +118,21 @@ router.delete('/:id', async (req, res) => {
|
|
|
118
118
|
})
|
|
119
119
|
});
|
|
120
120
|
|
|
121
|
+
router.get('/forget-password/:user_or_email', async (req, res) => {
|
|
122
|
+
const user_or_email = req.params.user_or_email;
|
|
123
|
+
const resetToken = randomUUID();
|
|
124
|
+
const resetLink = req.protocol + '://' + req.get('host') + '/frontend/reset-password?token=' + resetToken;
|
|
125
|
+
Users.sendResetPassword(user_or_email, resetToken, resetLink).then(() => {
|
|
126
|
+
res.json({
|
|
127
|
+
success: true,
|
|
128
|
+
message: ''
|
|
129
|
+
});
|
|
130
|
+
}).catch(err => {
|
|
131
|
+
res.status(500).json({
|
|
132
|
+
success: false,
|
|
133
|
+
message: `${err}`
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
|
|
121
138
|
module.exports = router;
|
package/src/models/users.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const nodemailer = require('nodemailer')
|
|
1
2
|
const config = require('../config')
|
|
2
3
|
const {db, schema } = require('./db')
|
|
3
4
|
const table = `${schema}.users`
|
|
@@ -98,6 +99,49 @@ class Users {
|
|
|
98
99
|
});
|
|
99
100
|
}
|
|
100
101
|
|
|
102
|
+
static sendResetPassword(user_or_email, resetToken, resetLink) {
|
|
103
|
+
return new Promise(async (resolve, reject) => {
|
|
104
|
+
let user = null;
|
|
105
|
+
try {
|
|
106
|
+
user = await db(table)
|
|
107
|
+
.where(function() {
|
|
108
|
+
this.where('username', user_or_email)
|
|
109
|
+
.orWhere('email', user_or_email);
|
|
110
|
+
})
|
|
111
|
+
.first();
|
|
112
|
+
} catch (e) {
|
|
113
|
+
return reject(e);
|
|
114
|
+
}
|
|
115
|
+
if(!user){
|
|
116
|
+
return resolve();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// TODO:
|
|
120
|
+
// Here we have to store the resetToken and expiration date (utc)
|
|
121
|
+
// in the database associated with the user
|
|
122
|
+
|
|
123
|
+
// Send the email
|
|
124
|
+
try{
|
|
125
|
+
const smtpConfig = config.smtp;
|
|
126
|
+
let transporter = nodemailer.createTransport(smtpConfig);
|
|
127
|
+
let mailOptions = {
|
|
128
|
+
from: smtpConfig.auth.user,
|
|
129
|
+
to: user.email,
|
|
130
|
+
subject: 'Password Reset',
|
|
131
|
+
text: `Hello ${user.name},\n\nYou can reset your password using the following link:\n${resetLink}\n\nIf you did not request a password reset, please ignore this email.\n\nBest regards,\nYour Company`
|
|
132
|
+
};
|
|
133
|
+
transporter.sendMail(mailOptions, (error, info) => {
|
|
134
|
+
if (error) {
|
|
135
|
+
return reject(error);
|
|
136
|
+
}
|
|
137
|
+
resolve();
|
|
138
|
+
});
|
|
139
|
+
}catch(e){
|
|
140
|
+
reject(e)
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
101
145
|
}
|
|
102
146
|
|
|
103
147
|
module.exports = Users
|