pioneers-token-manager 1.0.3 → 1.0.4
Sign up to get free protection for your applications and to get access to all the features.
- package/config.js +19 -0
- package/index.js +19 -15
- package/package.json +1 -1
package/config.js
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
statusCode = {
|
2
|
+
UNAUTHORIZED: 401
|
3
|
+
}
|
4
|
+
|
5
|
+
errors = {
|
6
|
+
UNAUTHORIZED:{
|
7
|
+
title: "UNAUTHORIZED",
|
8
|
+
message: "token not valid"
|
9
|
+
}
|
10
|
+
}
|
11
|
+
const contentTypes = {
|
12
|
+
JSON: 'application/json'
|
13
|
+
};
|
14
|
+
|
15
|
+
module.exports = {
|
16
|
+
statusCode,
|
17
|
+
errors,
|
18
|
+
contentTypes
|
19
|
+
}
|
package/index.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
+
const c = require('./config')
|
1
2
|
const jwt = require('jsonwebtoken');
|
2
|
-
const {extractToken} = require('./utilities/extract token')
|
3
|
+
const { extractToken } = require('./utilities/extract token')
|
3
4
|
|
4
5
|
class TokenManager {
|
5
6
|
constructor(config) {
|
@@ -12,23 +13,26 @@ class TokenManager {
|
|
12
13
|
const decoded = jwt.verify(token, this.tokenKey);
|
13
14
|
req.user = decoded;
|
14
15
|
} catch (err) {
|
15
|
-
|
16
|
-
|
16
|
+
res.statusCode = c.statusCode.UNAUTHORIZED;
|
17
|
+
res.setHeader('Content-Type', c.contentTypes.JSON);
|
18
|
+
res.end({message : c.error.UNAUTHORIZED.message});
|
19
|
+
return
|
20
|
+
}
|
17
21
|
return next();
|
18
22
|
}
|
19
|
-
|
20
|
-
|
21
|
-
|
23
|
+
generateToken = (payload) => {
|
24
|
+
const tokenKey = this.tokenKey;
|
25
|
+
const expireTime = this.expireTime;
|
22
26
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
27
|
+
const token = jwt.sign(
|
28
|
+
payload,
|
29
|
+
tokenKey,
|
30
|
+
{
|
31
|
+
expiresIn: expireTime
|
32
|
+
}
|
33
|
+
);
|
34
|
+
return token;
|
35
|
+
}
|
32
36
|
}
|
33
37
|
|
34
38
|
module.exports = { TokenManager }
|