How to set jwt token expiry time to maximum in nodejs?
To set expirey time in days: try this
var token = jwt.sign({email_id:'[email protected]'}, "Stack", {
expiresIn: '365d' // expires in 365 days
});
"expiresIn" should be a number of seconds or string that repesents a timespan eg: "1d", "20h",
Docs: jsonwebtoken
The exp
claim of a JWT is optional. If a token does not have it, it is considered that it does not expire
According to the documentation of https://www.npmjs.com/package/jsonwebtoken the expiresIn
field does not have a default value either, so just omit it.
There are no default values for expiresIn, notBefore, audience, subject, issuer. These claims can also be provided in the payload directly with exp, nbf, aud, sub and iss respectively, but you can't include them in both places.
var token = jwt.sign({email_id:'[email protected]'}, "Stack", {});
You can set expire time in number or string :
expressed in seconds or a string describing a time span zeit/ms.
Eg: 60, "2 days", "10h", "7d".A numeric value is interpreted as a seconds count. If you use a string be sure you provide the time units (days, hours, etc),
otherwise milliseconds unit is used by default ("120" is equal to "120ms").
var token = jwt.sign({email_id:'[email protected]'}, "Stack", {
expiresIn: "10h" // it will be expired after 10 hours
//expiresIn: "20d" // it will be expired after 20 days
//expiresIn: 120 // it will be expired after 120ms
//expiresIn: "120s" // it will be expired after 120s
});