1exports.verifyUserToken = (req, res, next) => {
2 let token = req.headers.authorization;
3 if (!token) return res.status(401).send("Access Denied / Unauthorized request");
4
5 try {
6 token = token.split(' ')[1] // Remove Bearer from string
7
8 if (token === 'null' || !token) return res.status(401).send('Unauthorized request');
9
10 let verifiedUser = jwt.verify(token, config.TOKEN_SECRET); // config.TOKEN_SECRET => 'secretKey'
11 if (!verifiedUser) return res.status(401).send('Unauthorized request')
12
13 req.user = verifiedUser; // user_id & user_type_id
14 next();
15
16 } catch (error) {
17 res.status(400).send("Invalid Token");
18 }
19
20}
21