Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: invalid 'instanceof' operand KeyObject #950

Open
RbrtDdds opened this issue Dec 2, 2023 · 13 comments
Open

TypeError: invalid 'instanceof' operand KeyObject #950

RbrtDdds opened this issue Dec 2, 2023 · 13 comments

Comments

@RbrtDdds
Copy link

RbrtDdds commented Dec 2, 2023

Description

TypeError: invalid 'instanceof' operand KeyObject

Reproduction

import { verify } from 'jsonwebtoken';

const jwtSecret = 'fooSecret';

export const JwtDecode = (token: string): Nullable<IJwtSignModel> => {
  if (!token) return null;
  return verify(token, jwtSecret) as IJwtSignModel;
};

// *Unhandled Runtime Error
TypeError: invalid 'instanceof' operand KeyObject

Environment

Please provide the following:
typescript: 5.2.2
jsonwebtoken: 9.0.2
@types/jsonwebtoken: 9.0.5 //tried with 9.0.2 too
Next js 14

@josephdburdick
Copy link

Also facing the same issue.

@QBT-VinayAdiga
Copy link

Same issue after updating to v9

@rajeshrah22
Copy link

I might take a look

@pranavajk1
Copy link

Yes I am also facing the same issue

@loicGR
Copy link

loicGR commented Feb 9, 2024

I am also facing the same issue in my Vue2 app. It works with version 8.5.1 but not with 9.0.0.

@milinddhamu
Copy link

this issue is mentioned here , as after update they added some additional parameter to sign an 'ordinary' or 'illegal' string.
Checkout here - https://stackoverflow.com/a/75033994/19933510

@milinddhamu
Copy link

this issue is mentioned here , as after update they added some additional parameter to sign an 'ordinary' or 'illegal' string.
Checkout here - https://stackoverflow.com/a/75033994/19933510

I hope this works well for guys and maybe can close this thread.

@loicGR
Copy link

loicGR commented Feb 15, 2024

this issue is mentioned here , as after update they added some additional parameter to sign an 'ordinary' or 'illegal' string.
Checkout here - https://stackoverflow.com/a/75033994/19933510

I hope this works well for guys and maybe can close this thread.

In my Vue2 application I only use the jwt.verify method. The proposed fix does not work for me. My tokens are signed in the backend with version 9

@milinddhamu
Copy link

this issue is mentioned here , as after update they added some additional parameter to sign an 'ordinary' or 'illegal' string.
Checkout here - https://stackoverflow.com/a/75033994/19933510

I hope this works well for guys and maybe can close this thread.

In my Vue2 application I only use the jwt.verify method. The proposed fix does not work for me. My tokens are signed in the backend with version 9

As per code example you are using an string 'fooSecret' which may not be correct as per v9 they added some strict rules for generating token so when we use older method it's breaking. Please if you got any lead help me out also about how this works as they demands more of proper values and their types as per docs.

Check this out at - https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v8-to-v9

@loicGR
Copy link

loicGR commented Feb 16, 2024

this issue is mentioned here , as after update they added some additional parameter to sign an 'ordinary' or 'illegal' string.
Checkout here - https://stackoverflow.com/a/75033994/19933510

I hope this works well for guys and maybe can close this thread.

In my Vue2 application I only use the jwt.verify method. The proposed fix does not work for me. My tokens are signed in the backend with version 9

As per code example you are using an string 'fooSecret' which may not be correct as per v9 they added some strict rules for generating token so when we use older method it's breaking. Please if you got any lead help me out also about how this works as they demands more of proper values and their types as per docs.

Check this out at - https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v8-to-v9

I don't see any answer to the problem in the documentation.
In the backend my token is signed with version 9 of jsonwebtoken in the following way:
jwt.sign(payload, this.privateKey, {expiresIn: '1h'})

In Vue2 frontend the token is decoded with version 8.5 of jsonwebtoken in the following way:
jwt.verify(token, publicKey)
And it works.

But if I upgrade to version 9 I get the error invalid 'instanceof' operand KeyObject

@milinddhamu
Copy link

this issue is mentioned here , as after update they added some additional parameter to sign an 'ordinary' or 'illegal' string.
Checkout here - https://stackoverflow.com/a/75033994/19933510

I hope this works well for guys and maybe can close this thread.

In my Vue2 application I only use the jwt.verify method. The proposed fix does not work for me. My tokens are signed in the backend with version 9

As per code example you are using an string 'fooSecret' which may not be correct as per v9 they added some strict rules for generating token so when we use older method it's breaking. Please if you got any lead help me out also about how this works as they demands more of proper values and their types as per docs.

Check this out at - https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v8-to-v9

I don't see any answer to the problem in the documentation.
In the backend my token is signed with version 9 of jsonwebtoken in the following way:
jwt.sign(payload, this.privateKey, {expiresIn: '1h'})

In Vue2 frontend the token is decoded with version 8.5 of jsonwebtoken in the following way:
jwt.verify(token, publicKey)
And it works.

But if I upgrade to version 9 I get the error invalid 'instanceof' operand KeyObject

So issue is when you're verifying, is it generating token perfectly. If you sign any illegal key in v9 it'll show same error. As i used it in next.js not tried in vue.

@adamsocrat
Copy link

As I have understood jsonwebtoken package is not designed for browsers and you should use packages that designed for client-side as such jwt.io javaScript libraries

@loicGR
Copy link

loicGR commented Mar 20, 2024

As I have understood jsonwebtoken package is not designed for browsers and you should use packages that designed for client-side as such jwt.io javaScript libraries

That's what I did. I opted for jwt-decode and jwt-encode

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants