Having trouble with JsonWebToken; JsonWebToken Error: JWT must be provided

I'm building my first SPA project with Vue.

I decided to go with NodeJS for the back-end, however, I'm having a headache building the login function with the JsonWebToken.

I had wrote some codes to see how JWT works and when I tried to see how JWT gets verified, server gave me an error.

JsonWebTokenError: jwt must be provided
at Object.module.exports [as verify] (c:\dir\node_modules\jsonwebtoken\verify.js:39:17)
at c:\projects\practice\demo\back\server.js:34:17

Below is the code for my server.js

This is the code for importing the stuff.

const express = require('express');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const api = express();
api.use(bodyParser.json());
api.use(bodyParser.urlencoded({ extended: true }));

This is for the API for issuing JWT.

api.post('/secure', function (req, res) {
const token = jwt.sign({ user: {id:1, name:'ME!', role: 'average'} }, 'dsfklgj');
console.log(token);
res.json({jwt: token});
});

This is the API for checking JWT.

api.post('/check/post', function (req, res) {
const token = req.body.jwt;
const x = jwt.verify(token, 'dsfklgj', function (err, decoded) {
if (err) throw err;
console.log(decoded);
});
if (x != true) {
res.json({ auth: false });
}else {
res.json({ auth: true });
}
});
2

3 Answers

jwt must be provided

This error happens when the coming token is null or empty.

4

Make sure that the Authorization header has a value of Bearer <Access Token>. Bearer < white space > < Access Token >

0

It may be you have not defined jwt in specific file or it is null or empty. Therefore you are getting an error. I just test your code and it works for me. It may be that you are not sending jwt token into post request correctly.

const express = require('express');
const jwt = require('jsonwebtoken');
const bodyParser = require('body-parser');
const http = require('http');
const api = express();
api.use(bodyParser.json());
api.use(bodyParser.urlencoded({ extended: true }));
api.post('/secure', function(req, res) { const token = jwt.sign({ user: { id: 1, name: 'ME!', role: 'average' } }, 'dsfklgj'); console.log(token); res.json({ jwt: token });
});
api.post('/check/post', function(req, res) { const token = req.body.jwt; console.log('token: ' + token); const x = jwt.verify(token, 'dsfklgj', function(err, decoded) { if (err) throw err; console.log(decoded); }); console.log(x); if (x != true) { res.json({ auth: false }); } else { res.json({ auth: true }); }
});
api.set('port', 3000);
var server = http.createServer(api);
server.listen(api.get('port'), function() { console.log("Express server listening on port " + api.get('port'));
});

BTW there is no way to test it like like this const x = jwt.verify(token, 'dsfklgj', function (err, decoded) {. Either write it in Sync way or check condition in async callback function. In your case, x will be undefined and no guarantee when it will run.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like