Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 });
}
});
like image 964
HelloWorld Avatar asked Jan 27 '18 14:01

HelloWorld


People also ask

What is Jsonwebtoken in node JS?

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.

Where do I put JWT token in Postman?

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.

What is Jsonwebtoken?

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.


2 Answers

jwt must be provided

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

like image 137
Mohammad Rajabloo Avatar answered Oct 13 '22 19:10

Mohammad Rajabloo


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.

like image 31
mabc224 Avatar answered Oct 13 '22 20:10

mabc224