Reputation: 615
I am going to create a token of form data using jsonwebtoken
in my React project.
import jwt from 'jsonwebtoken';
const MyForm = () => {
const submitForm = (e) => {
e.preventDefault();
const data = { name: 'name', email: 'email', subject: 'subject', message: 'message' };
const token = jwt.sign(data, 'qwerty');
console.log(token);
}
return(
<form onSubmit={submitForm}>
...
</form>
);
}
export default MyForm;
Following error is occurred.
TypeError: Right-hand side of 'instanceof' is not an object
push../node_modules/jsonwebtoken/sign.js.module.exports [as sign]
../node_modules/jsonwebtoken/sign.js:108
105 | return failure(new Error('secretOrPrivateKey must have a value'));
106 | }
107 |
> 108 | if (secretOrPrivateKey != null && !(secretOrPrivateKey instanceof KeyObject)) {
| ^ 109 | try {
110 | secretOrPrivateKey = createPrivateKey(secretOrPrivateKey)
111 | } catch (_) {
View compiled
submitEmail
../MyForm.js:22
19 | e.preventDefault();
20 |
21 | const data = { name: 'name', email: 'email', subject: 'subject', message: 'message' };
> 22 | const token = jwt.sign(data, 'qwerty');
| ^ 23 | console.log(token)
24 |
25 |
Why is this giving me that error?
I have tried to solve this problem but no results yet.
And any help to fix it is appreciated. Thanks.
Upvotes: 25
Views: 34652
Reputation: 396
Alternatively you can use the following package. I used it in nextjs . Worked like a charm. I am sure it will work on react as well.
Package - Jose
This stack overflow answer as well as the example mentioned here assisted me on how to use that package.
Upvotes: 1
Reputation: 1
I experienced a similar issue with the verify module, verifying in the frontend the issue was with the apikey. It didnt correspond with the apikey at the backend
Upvotes: -1
Reputation: 1
I had the same issue, I was using node version v10.16.0, I uninstalled it and download node version v18.16.0. Then it works well
Upvotes: -1
Reputation: 1
I ran into this as well. Nothing suggested so far worked for me until I also made this change to use require statement rather than an import.
const jwt = require('jsonwebtoken');
Upvotes: 0
Reputation: 41
In this case, you need to use the latest node version, so please first check your node version this error can get in version 10.*.*
So update your Nodejs version
Upvotes: 2
Reputation: 696
I had the same issue. The issue being that version 9.0.0 of the jsonwebtoken package does not support node version 11 and below, so I solved it by downgrading the version of the jsonwebtoken package.
I did so by changing the jsonwebtoken dependency from 9.0.0 to 8.5.1 inside the package.json file, then I ran npm update inside my terminal.
Upgrading your node version might be recommended here, but I haven't quite managed to update my node version because of an npm issue, so this is only a workaround.
Checkout the following article explaining the migration from v8 to v9 of the jsonwebtoken package. https://github.com/auth0/node-jsonwebtoken/wiki/Migration-Notes:-v8-to-v9
Upvotes: 41
Reputation: 11
In the latest jsonwebtoken, v9: might want to try...
let secret = 'qwerty';
const token = jwt.sign(data, secret.toString('utf-8'));
additionally, you may need to add params to force older behavior...
const token = jwt.sign(data, secret.toString('utf-8'), { algorithm: 'HS256', allowInsecureKeySizes: true, allowInvalidAsymmetricKeyTypes: true });
Upvotes: 0
Reputation: 87
jsonwebtoken
is a Node module. In other words, it does not work on browsers very well. See this issue here. This page offers good alternatives for client-side jwt operations.
Upvotes: 5