All files / bc-mongodb-implementation-demo/src/middleware verifyAdminToken.js

86.66% Statements 13/15
70% Branches 7/10
100% Functions 2/2
86.66% Lines 13/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 292x 2x 2x   2x   1x   1x 1x 1x 1x         1x 1x   1x               2x  
const { StatusCodes } = require('http-status-codes')
const jwt = require('jsonwebtoken')
const JWT_SECRET = process.env.JWT_SECRET_KEY
 
const verifyAdminToken = (req, res, next) => {
  let token
  const authHeader = req.headers.Authorization || req.headers.authorization
 
  Eif (authHeader && authHeader.startsWith('Bearer')) {
    token = authHeader.split(' ')[1]
    jwt.verify(token, JWT_SECRET, (err, decoded) => {
      Iif (err) {
        return res
          .status(StatusCodes.UNAUTHORIZED)
          .json({ message: 'Invalid token' })
      }
      req.user = decoded.user
      next()
    })
    Iif (!token) {
      return res
        .status(StatusCodes.UNAUTHORIZED)
        .json({ message: 'User not authorized or token is missing' })
    }
  }
}
 
module.exports = verifyAdminToken