Issues (7)

1 1
var jwt = require('jsonwebtoken');
2
3
/**
4
 * Middleware for verifying JWT tokens
5
 *
6
 * Using the jsonwebtoken-library, available options can be found here:
7
 * https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback
8
 *
9
 * @param {*} options - Options
10
 *
11
 * @return error or next()
12
 */
13 1
module.exports = function(secret, options = {}) {
14 1
    return (req, res, next) => {
15 4
        const token = req.body.token || req.query.token || req.headers['x-access-token'];
16
17 4
        if (!token) {
18 1
            return res.status(403).send({
19
                success: false,
20
                status: 403,
21
                title: 'NoTokenProvided',
22
                description: "Forbidden, missing token."
23
            });
24
        }
25
26 3
        jwt.verify(token, secret, options, (err, decoded) => {
27 3
            if (err) {
28 2
                return res.status(403).send({
29
                    success: false,
30
                    status: 403,
31
                    title: err.name,
32
                    description: `Forbidden. ${err.message}`
33
34
                });
35
            }
36
37 1
            req.decoded = decoded;
38 1
            next();
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
39
        });
0 ignored issues
show
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
40
    };
41
};
42