pioneers-token-manager 1.0.0 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
package/config.js CHANGED
@@ -1,22 +1,19 @@
1
- const statusCodes = {
2
- SUCCESS: 200,
1
+ statusCode = {
3
2
  UNAUTHORIZED: 401
4
- };
5
-
6
- const contentTypes = {
3
+ }
4
+
5
+ errors = {
6
+ UNAUTHORIZED:{
7
+ title: "UNAUTHORIZED",
8
+ message: "token not valid"
9
+ }
10
+ }
11
+ const contentTypes = {
7
12
  JSON: 'application/json'
8
- };
9
-
10
- const errors = {};
11
-
12
- const messages = {
13
- loginFailed: 'Wrong username or password!',
14
- invalidToken: 'Invalid token!',
15
- };
16
-
17
- module.exports = {
18
- statusCodes,
19
- contentTypes,
13
+ };
14
+
15
+ module.exports = {
16
+ statusCode,
20
17
  errors,
21
- messages,
22
- };
18
+ contentTypes
19
+ }
package/index.js CHANGED
@@ -1,8 +1,6 @@
1
-
2
- const c = require('./config');
1
+ const c = require('./config')
3
2
  const jwt = require('jsonwebtoken');
4
- const { sendFail } = require('./utilities/responseHandler');
5
- const {extractToken} = require('./utilities/extract token')
3
+ const { extractToken } = require('./utilities/extract token')
6
4
 
7
5
  class TokenManager {
8
6
  constructor(config) {
@@ -11,31 +9,30 @@ class TokenManager {
11
9
  }
12
10
  verifyToken = (req, res, next) => {
13
11
  const token = extractToken(req);
14
- if (!token) {
15
- sendFail(res, c.statusCodes.UNAUTHORIZED, { message: c.messages.invalidToken });
16
- return;
17
- }
18
12
  try {
19
13
  const decoded = jwt.verify(token, this.tokenKey);
20
14
  req.user = decoded;
21
15
  } catch (err) {
22
- return sendFail(res, c.statusCodes.UNAUTHORIZED, { message: c.messages.invalidToken })
23
- }
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
+ }
24
21
  return next();
25
22
  }
26
- generateToken = (payload) => {
27
- const tokenKey = this.tokenKey;
28
- const expireTime = this.expireTime;
23
+ generateToken = (payload) => {
24
+ const tokenKey = this.tokenKey;
25
+ const expireTime = this.expireTime;
29
26
 
30
- const token = jwt.sign(
31
- payload,
32
- tokenKey,
33
- {
34
- expiresIn: expireTime
35
- }
36
- );
37
- return token;
38
- }
27
+ const token = jwt.sign(
28
+ payload,
29
+ tokenKey,
30
+ {
31
+ expiresIn: expireTime
32
+ }
33
+ );
34
+ return token;
35
+ }
39
36
  }
40
37
 
41
38
  module.exports = { TokenManager }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pioneers-token-manager",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "description": "a module to verify token",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -10,7 +10,6 @@
10
10
  "author": "mohammad vatani",
11
11
  "license": "ISC",
12
12
  "dependencies": {
13
- "jsonwebtoken": "^8.5.1",
14
- "pioneers-server": "^1.0.2"
13
+ "jsonwebtoken": "^8.5.1"
15
14
  }
16
15
  }
package/client.js DELETED
File without changes
@@ -1,13 +0,0 @@
1
- const c = require('../config');
2
-
3
- module.exports.sendOk = (res, data) => {
4
- res.statusCode = c.statusCodes.SUCCESS;
5
- res.setHeader('Content-Type', c.contentTypes.JSON);
6
- res.end(JSON.stringify(data));
7
- }
8
-
9
- module.exports.sendFail = (res, statusCode, data) => {
10
- res.statusCode = statusCode;
11
- res.setHeader('Content-Type', c.contentTypes.JSON);
12
- res.end(JSON.stringify(data));
13
- }