How can I use fastify-jwt to sign a token with expiredtime?

Viewed 850

If we dont need expiredIn,just use this way that I can create a token

const token = await reply.jwtSign(userJSON)

But I want to give a time that token should disabled:expiredIn! What should I do to give a expiredTime.

1 Answers

According to the documentation

expiresIn: 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").

you have to use the expiresInoption in the call to jwtSign, e.g.:

const token = await reply.jwtSign(userJSON, { expiresIn: '1h' })

for a token that expires in 1 hour.

Related