All files / src/auth userAuth.js

81.81% Statements 9/11
66.66% Branches 4/6
100% Functions 1/1
80% Lines 8/10

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        52x 52x 52x 50x 50x 50x 50x         2x    
import extractAuthFromRequestHeaders from "../utilities/extractAuthFromRequestHeaders.js";
import { verifyJWT } from "../utilities/jwtUtilities.js";
 
export default async function userAuth(req, res, next) {
    const unAuthorizedMsg = { message: "You need some credentials first to access this api" }
    const { accessToken } = extractAuthFromRequestHeaders(req)
    if (accessToken) {
        try {
            const token = verifyJWT(accessToken)
            if (token.userId || token.adminId) {
                req.userId = token.userId; next();
            } else E{ res.status(401).json({ message: "There is problem in your credentials" }) }
        } catch {
            res.status(401).json(unAuthorizedMsg)
        }
    } else res.status(401).json(unAuthorizedMsg)
};