-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
60 lines (50 loc) · 1.68 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// * Import needed stuffs
const jwt = require('jsonwebtoken');
const logger = require('./logger');
const { UnprocessableEntity } = require('./errors/UnproccessableEntity');
// TODO: Extract the env values
const { JWT_ACCESS_TOKEN } = process.env;
/**
* @desc To generate the token for accessing our webapp
*
*@param {{id}} config - This is contains user encryption informations. ex. roleId, email.
*@param {String} exp - Set expiration time if you want.
*/
exports.getAccessToken = async (config, exp) => {
try {
return jwt.sign(config, JWT_ACCESS_TOKEN || '', { expiresIn: exp });
} catch (err) {
logger.error(err);
throw new UnprocessableEntity('Access token not generated!');
}
};
/**
*
* @param req Express request object
* @param res Express response object
* @param next Express next function for move on through middleware
*/
exports.authenticator = (req, res, next) => {
// ? Get the auth header from request payload
const authHeader = req.headers.authorization;
// ! check if the header present or not
if (!authHeader)
return res
.status(400)
.json({ result: false, error: 'Forbidden', message: 'Header is missing.' });
// TODO: extract the token from header
const token = authHeader.split(' ')[1];
if (!token)
return res
.status(400)
.json({ result: false, error: 'Forbidden', message: 'Token is missing.' });
// * Check if the token is valid or not
jwt.verify(token, JWT_ACCESS_TOKEN || '', (err, user) => {
// ! If not throw error
if (err) return res.status(403).json({ result: false, error: 'Unauthorized', message: err });
// TODO: Store into users object
req.user = user;
// TODO: move on to next middleware functions
return next();
});
};