JsonWebTokenError: invalid token

Nao Kawakami
Jul 29, 2021

Troubleshoot for JWT.

I got above error and my code was below.

var jwt = require('jsonwebtoken')const tokenString = `eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvc2UiLCJwYXNzd29yZCI6InJlZCIsImlhdCI6MTYyNzU4NjczMH0.SuLJGjGJy14d2CUD9hdtWk5M6EGa53zv_TNcPeGMd98`const data = jwt.verify(tokenString, SECRET_KEY) // error hereconsole.log(data)

I tested out how JWT worked. I generated token with jwt.sign() then checked the verify method.

SECRET_KEY and token was generated as below. I logged the token string on console, then I assigned the value into tokenString.

//jwt.jsconst SECRET_KEY = 'thisissecret'const token = jwt.sign({username:'rose', password:'red'}, SECRET_KEY)

Then I got error JsonWebTokenError: invalid token

I fixed by modifying the tokenString into single line as below.

const tokenString = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InJvc2UiLCJwYXNzd29yZCI6InJlZCIsImlhdCI6MTYyNzU4NjczMH0.SuLJGjGJy14d2CUD9hdtWk5M6EGa53zv_TNcPeGMd98'

Multiple lines cannot be assigned to verify

--

--