Skip to content
Advertisement

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:dirnode_modulesjsonwebtokenverify.js:39:17)
at c:projectspracticedemobackserver.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 });
}
});

Advertisement

Answer

jwt must be provided

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

Advertisement