Search code examples
node.jsjwtexpress-jwt

JWT invalid signature issue with NodeJS


I know this issue is common I have found several posts here and researched all over the internet but still no luck at all and I have spend half of the day.

Here is my NodeJS code:

jwt.sign(jwtData, config.secret, {
                    expiresIn: 60 * 60 * 24 // expires in 24 hours
                }, (err, token) => {
                    res.access_token = token
                    console.log("------------", res)
                    callback(null, res)
                })

jwt.verify(res.access_token, config.secret, function(err, decoded) {
                                if (err) {
                                   console.log("err----", err)
                                } else {
                                    cb({
                                        message: 'Token Exist'
                                    })
                                }
                            });

Error says : JsonWebTokenError: invalid signature

Also, I am able verify token on official site and it is working over there with valid signature.

I have checked my secret whether it is a string or not and it is a string.

Thanks in advance!!!

EDIT:

res.access_token--- eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MjUsInJvbGVfaWQiOjIsImlzX3BheW1lbnRfZmFpbCI6MCwiaXNfcGFpZCI6MSwiaXNfb3duZXIiOjEsImZpcnN0X25hbWUiOiJTYWdhciIsImxhc3RfbmFtZSI6IlZpcnBhcmEiLCJpYXQiOjE1ODMzMjU4NDYsImV4cCI6MTU4MzQxMjI0Nn0.8LNFXPwSPjjbZE9IWh0p7Xri8t

config.secret----- #&%S0l!dP1vE*@!%

Solution

  • Problem is that I was storing the token in varchar(255) data type and a token were trimming automatically due to limitation of the datatype.

    So, I have just changed data type to text and it was working perfectly.

    Thanks to @jps