Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

414
Vistas
How to change value in String in javascript

I am generating JWT token and in there I have to pass expirein arguement which should be in string and that value is coming from credentials. this is code please tell me how can I change this value to a string.

const generateToken = ({ id, errorMessage, secret }) => {
  try {
    const token = jwt.sign({ id }, secret, {
      expiresIn:JSON.stringify(`${config.SESSION_DURATION * 60 * 1000}`),
    });
    return token;
  } catch (err) {
    throw errorMessage || err;
  }
};

this above code generating a error like this

"\"expiresIn\" should be a number of seconds or string representing a timespan eg: \"1d\", \"20h\", 60"

and this is how I am passing this token as a cookies maybe this error come from here

res
        .cookie("jwt_auth", token, {
          maxAge: config.SESSION_DURATION * 60 * 1000,    //it should be in miliseconds 
          // maxAge: 3600000,  
          httpOnly: true,
          sameSite: true,
          secure: config.NODE_ENV === "development",
        })
        .redirect(config.CLIENT_URL);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

It gives a type error.

It Expects a number and you are passing a string (also it's seconds not millis).

When its string then should be '1s', '2m', ... its an npm library ms https://www.npmjs.com/package/ms.

Either use that ms compatible value or number.

This should work.

const generateToken = ({ id, errorMessage, secret }) => {
  try {
    const token = jwt.sign({ id }, secret, {
      expiresIn: Number(config.SESSION_DURATION),
    });
    return token;
  } catch (err) {
    throw errorMessage || err;
  }
};
about 4 years ago · Juan Pablo Isaza Denunciar

0

you should be able to pass

let x = config.SESSION_DURATION * 60 * 1000
...
maxAge: x.toString()
...

or use the string '365d' as the error message is pointing out

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda