const jwt = require('jsonwebtoken');

const middleware = (req, res, next) => {
    if (req.path === "/login") {
        next();
    } else {
        const token = req.headers.authorization.replace(/"/g, "");
        try {
            const isTokenValid = jwt.verify(token, "gohadanchisecretkey");
            next();
        } catch (e) {
            res.status(401).send({
                error: {
                    message: "user not logged in"
                }
            });
            console.log(e);
        }
    }
};

module.exports = middleware;