#jsonwebtoken not working in bun

1 messages · Page 1 of 1 (latest)

warm sphinx
#

Any help will be much appreciated.
if I can't fix this, i have to go back to nodejs...
running [email protected] and jsonwebtoken@"9.0.2"

It works in node

import jwt from 'jsonwebtoken'
// or
const jwt = require('jsonwebtoken')

jwt.sign({id: 1}, 'SECRET')

gives me the following error:

if (!secretOrPrivateKey && options.algorithm !== 'none') {
111 |     return failure(new Error('secretOrPrivateKey must have a value'));
112 |   }
113 |
114 |   if (secretOrPrivateKey != null && !(secretOrPrivateKey instanceof KeyObject))
rigid harness
warm sphinx
#

😢

warm sphinx
#

I made a compromise, as running a typescript project with node would take me more time to configure:

const { PORT, JWT_SECRET } = process.env;
const JWT_PORT = +PORT + 999;
const URL = `http://127.0.0.1:${JWT_PORT}`;

export const sign = async (data) => {
  const json = JSON.stringify(data);
  console.log('signing', json);
  const res = await fetch(`${URL}/sign/${json}`);
  const token = await res.text();
  return token;
};

export const verify = async (token) => {
  const res = await fetch(`${URL}/verify/${token}`);
  const data = await res.json();
  return data;
};

let jwtProcess;

const connect = async () => {
  try {
    const isAlive = await fetch(URL);
  } catch (ex) {
    if (jwtProcess) jwtProcess.kill();
    console.log('connecting to jwtProcess');
    jwtProcess = Bun.spawn(['node', './nodejs/jwt.js', JWT_PORT, JWT_SECRET]);
    console.log('connected to jwtProcess');
  }
};

connect();
setInterval(connect, 10000);

and my nodejs/jwt.js file:

import express from 'express';
import jwt from 'jsonwebtoken';

const [, , PORT, SECRET] = process.argv;
console.log(PORT, SECRET);

const app = express();

app.get('/', (req, res) => res.send('ok'));

app.get('/sign/:json', (req, res) => {
  try {
    res.send(jwt.sign(req.params.json, SECRET));
  } catch (ex) {
    res.send(null);
  }
});

app.get('/verify/:token', (req, res) => {
  try {
    res.send(jwt.verify(req.params.token, SECRET));
  } catch (ex) {
    res.send(null);
  }
});

app.listen(PORT);

tall fossil
#

You can use the jose package instead, it works on bun

warm sphinx
tall fossil
#
#

It's what I've been using instead

warm sphinx
#

angular and google maps???

tall fossil
#

I don't know how I got that hold on

#
tall fossil
#

For your jwt secret it has to be in encoded text so you have to do

const encoder = new TextEncoder();
const JWT_SECRET = encoder.encode(Bun.env.JWT_SECRET);
warm sphinx
#

haha nice. thank you. there's not a lot of instructions there

tall fossil
#

Here's my encode

const encoder = new TextEncoder();
const JWT_SECRET = encoder.encode(Bun.env.JWT_SECRET);

return new jose.SignJWT({ id: this._id })
  .setProtectedHeader({ alg: 'HS256' })
  .setExpirationTime(Bun.env.JWT_EXPIRE as string)
  .sign(JWT_SECRET);

And decode

const encoder = new TextEncoder();
const JWT_SECRET = encoder.encode(Bun.env.JWT_SECRET);

const { payload } = await jose.jwtVerify(token, JWT_SECRET);
warm sphinx
#

thank you, ill try it out

#

my solution works but it's super kamikaze

warm sphinx
tall fossil
#

You can set a string on milliseconds

#

Like 30d

warm sphinx
#

thank you

tall fossil
#

No problem