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.
Files changed (3) hide show
  1. package/config.js +19 -0
  2. package/index.js +19 -15
  3. 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
- generateToken = (payload) => {
20
- const tokenKey = this.tokenKey;
21
- const expireTime = this.expireTime;
23
+ generateToken = (payload) => {
24
+ const tokenKey = this.tokenKey;
25
+ const expireTime = this.expireTime;
22
26
 
23
- const token = jwt.sign(
24
- payload,
25
- tokenKey,
26
- {
27
- expiresIn: expireTime
28
- }
29
- );
30
- return token;
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 }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pioneers-token-manager",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "a module to verify token",
5
5
  "main": "index.js",
6
6
  "scripts": {