Im trying to create an application with express and JWT (JsonWebToken). It works localy but when i try to use it on my server at home it gives an error. I spend like an hour trying to find a solution but nothing works.
TypeError: Right-hand side of 'instanceof' is not an object
at Object.module.exports [as sign] (/home/container/node_modules/jsonwebtoken/sign.js:108:58)
at server.post (/home/container/server.js:51:9)
at Layer.handle [as handle_request] (/home/container/node_modules/express/lib/router/layer.js:95:5)
at next (/home/container/node_modules/express/lib/router/route.js:144:13)
at Route.dispatch (/home/container/node_modules/express/lib/router/route.js:114:3)
at Layer.handle [as handle_request] (/home/container/node_modules/express/lib/router/layer.js:95:5)
at /home/container/node_modules/express/lib/router/index.js:284:15
at Function.process_params (/home/container/node_modules/express/lib/router/index.js:346:12)
at next (/home/container/node_modules/express/lib/router/index.js:280:10)
at /home/container/node_modules/body-parser/lib/read.js:137:5
It is a really a simple application but i can't find out why it gives the error above ^^
This is my code:
const cors = require('cors');
const express = require('express');
const bodyParser = require('body-parser');
const server = express();
const port = 3000 || process.env.PORT;
server.options('*', cors());
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: false }));
const jwt = require('jsonwebtoken');
server.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
const posts = [
{
user: 'david',
title: 'post'
},
{
user: 'bryan',
title: 'post2'
}
];
server.get('/posts', authenticateToken, (req, response) => {
response.json(posts.filter(post => post.user === req.user));
});
server.post('/login', (req, res) => {
const {username, password} = req.body;
if(username == null) return res.sendStatus(401);
jwt.sign(username, 'test', (err, token) => {
if(err) {
console.log(err);
return res.sendStatus(403);
}
console.log(accessToken);
res.json({accessToken: accessToken});
});
});
function authenticateToken(req, res, next) {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if(token == null) return res.sendStatus(401);
jwt.verify(token, 'test', (err, user) => {
if(err) return res.sendStatus(403);
req.user = user;
next();
});
}
I cannot find a solution on any website.
CodePudding user response:
I had the same problem. I found out that the problem appeared when running the npm audit fix
command to get rid of the vulnerabilities.
The process updated passport module and that seemed to be the cause of the problem. I went back to the settings before applying the fix and everything worked fine again.
Diff:
"passport": "~0.2.0",
"passport": "^0.6.0",
"passport-jwt": "^4.0.0",
"passport-jwt": "^4.0.1",
Fairly old node version: 10.16.0
I hope it helps
CodePudding user response:
I updated the node version on the server and it fixed the error.