Search code examples
expressjwtthunderclient

With thunder client testing time, I am using jwt token, but I am getting error and unable to access authentication


When trying to check with thunder client with jwt token, I am getting error message

 "invalid token"

Whatever Token generate successfully, but with this token I can't authenticate & getting error

Here is the token generate code, it works fine

const express = require('express');
const app = express();
const connectDB = require('./db/db');
const User = require('./models/User');
const jwt = require('jsonwebtoken');
app.use(express.json());

app.post('/token', async (req, res, next) => {

    try {
        const token = jwt.sign(user._doc, 'secret-key', { expiresIn: '2h' })     

        return res.status(200).json({ message: "Token generated successfully", token });
    } catch (e) {
        next(e);
    }
});

For reference, successfully generated token:

{
  "message": "Token generated successfully",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NjYzNDVkMDRjODMxZDA4YWQ5MzQ5NzEiLCJuYW1lIjoia2F0ZSIsImVtYWlsIjoia2F0ZUBrLmNvbSIsInJvbGVzIjpbIkFETUlOIl0sImFjY291bnRTdGF0dXMiOiJQRU5ESU5HIiwiX192IjowLCJpYXQiOjE3MTgwMzU3NTEsImV4cCI6MTcxODAzOTM1MX0.qkF_UxmQs34uYLeOi9rj3pS8y-FSLX0QNjLCzFz_7Ls"
}

Thunder Client Testing, here is the parameter what I am using

Here is the snippet details:

Headers- > Http headers

Accept: */*
agent: Thunder Client
Authorization: Bearer {{token}}

Product route code:

app.get('/product', async (req, res) => {
    let token = req.headers.authorization
    console.log('token: ' + token)

    if (!token) {
        return res.status(401).json({ message: 'Un-authorization access' });
    }

    try {
        token = token.split(' ')[1];
        const decoded = jwt.verify(token, 'secret-key');
        const user = await User.findById(decoded._id);
        console.log(user);

        if (!user) {
            return res.status(401).json({ message: 'Un-authorization user access' });
        }

    } catch (e) {
        return res.status(400).json({ message: 'Invalid Token' });
    }

    return res.status(200).json({ message: 'I am a private route' });
})

Problem clearly visible here, the error message is:

Invalid token:

{
  "message": "Invalid Token"
}
Status: 400 Bad Request
Size: 27 Bytes
Time: 16 ms

My steps:

  • I took different token with different user, but error is the same with the thunder client

  • I upgrade the thunder client version but same result

  • New & different token unable to work


Solution

  • 1st step:

    Open Thunder Client, and Hit Route: /product

    Auth-> Bearer-> Bearer Token: put token / in your case eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NjYzNDVkMDRjODMxZDA4YWQ5MzQ5NzEiLCJuYW1lIjoia2F0ZSIsImVtYWlsIjoia2F0ZUBrLmNvbSIsInJvbGVzIjpbIkFETUlOIl0sImFjY291bnRTdGF0dXMiOiJQRU5ESU5HIiwiX192IjowLCJpYXQiOjE3MTgwMzU3NTEsImV4cCI6MTcxODAzOTM1MX0.qkF_UxmQs34uYLeOi9rj3pS8y-FSLX0QNjLCzFz_7Ls

    2nd step:

    Then start your above mentioned steps

    Hope it helps you