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 });
}
});
JWTs are mainly used for authentication. After a user signs in to an application, the application then assigns JWT to that user. Subsequent requests by the user will include the assigned JWT. This token tells the server what routes, services, and resources the user is allowed to access.
Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.
JWT, or JSON Web Token, is an open standard used to share information between two parties securely — a client and a server. In most cases, it's an encoded JSON containing a set of claims and a signature.
jwt must be provided
This error happens when the coming token is null or empty.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With