auth0 / express-jwt

connect/express middleware that validates a JsonWebToken (JWT) and set the req.user with the attributes

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to catch 404 error?

mgsod opened this issue · comments

commented

Please do not report security vulnerabilities here. The Responsible Disclosure Program details the procedure for disclosing security issues.

Thank you in advance for helping us to improve this library! Please read through the template below and answer all relevant questions. Your additional work here is greatly appreciated and will help us respond as quickly as possible. For general support or usage questions, use the Auth0 Community or Auth0 Support. Finally, to avoid duplicates, please search existing Issues before submitting one here.

By submitting an Issue to this repository, you agree to the terms within the Auth0 Code of Conduct.

Description

I used express-jwt and set some routes. But when i visit a path that not in my routes. It get a 'UnauthorizedError'. But it should be 404 .

Reproduction

app.use(expressJwt({
    algorithms: ['HS256'],
    secret: "secret12345",
    requestProperty: 'authorization'
}).unless({
    path: ["/", "/login"]
}));
app.use(express.static('./'))

//api/v1/login
app.post('/login', function (req, res) {
    res.json({
        status: 'ok',
    })
})
app.get('/list', function (req, res) {
    res.json({
        status: 'ok',
    })
})
app.get('/', (req, res) => res.send('Hello World!'))

// When visit /xxxxx ,how to catch 404 error not UnauthorizedError
/*
app.use((err, req, res) => {
    res.sendStatus(404);
})
*/

app.use(function (err, req, res, next) {
    if (err.name === 'UnauthorizedError') {
        res.json({
            message: 'xxxxx',
            code: 401
        })
        return
    }
    res.status(err.status || 500);
    res.render('error');
});
`

### Environment

> Please provide the following:

- **Version of this library used: 6.0.0
- **Version of the platform or framework used, if applicable: express
- **Other relevant versions (language, server software, OS, browser):windows
- **Other modules/plugins/libraries that might be involved:express