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);
CodePudding user response:
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;
}
};
CodePudding user response:
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
