Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

415
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda