applay-utils 1.2.12 → 1.2.14
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 +1 -1
- package/utils/jwt.js +21 -24
package/package.json
CHANGED
package/utils/jwt.js
CHANGED
|
@@ -1,31 +1,28 @@
|
|
|
1
1
|
const jwt = require('jsonwebtoken');
|
|
2
|
-
var key=process.env.key;
|
|
2
|
+
var key = process.env.key;
|
|
3
3
|
var keyportal = process.env.keyportal;
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
var utils = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
});
|
|
28
|
-
return token;
|
|
29
|
-
}
|
|
7
|
+
'setKey': (newkey) => { key = newkey; },
|
|
8
|
+
'setKeyPortal': (newkey) => { keyportal = newkey; },
|
|
9
|
+
'checkPortal': (keyCheck) => keyCheck == keyportal,
|
|
10
|
+
'auth': (req, res, next) => {
|
|
11
|
+
const token = req.headers['x-access-token'];
|
|
12
|
+
if (!token) return res.status(401).json({ auth: false, message: 'Sem token definido.' });
|
|
13
|
+
if (!key) return res.status(401).json({ auth: false, message: 'Sem key definida.' });
|
|
14
|
+
jwt.verify(token, key, (err, decoded) => {
|
|
15
|
+
if (err) return res.status(401).json({ auth: false, message: 'Falha na autenticação do token.' });
|
|
16
|
+
req.tokenId = decoded.id;
|
|
17
|
+
next();
|
|
18
|
+
});
|
|
19
|
+
},
|
|
20
|
+
'login': (id, time) => {
|
|
21
|
+
if (!key) return null;
|
|
22
|
+
const token = jwt.sign({ id }, key, {
|
|
23
|
+
expiresIn: time || (60 * 30) // segundos
|
|
24
|
+
});
|
|
25
|
+
return token;
|
|
26
|
+
}
|
|
30
27
|
}
|
|
31
28
|
module.exports = utils;
|