Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsonwebtoken.sign() fails with expiresIn option set

I need to get a token by signing a user ID with JWT:

var token = jwt.sign(accounts[request.headers.login].id, privateKey, {expiresIn: 60});

Where id and privateKey are strings.

The error is Error: Uncaught error: "expiresIn" should be a number of seconds or string representing a timespan eg: "1d", "20h", 60. If I remove options object at all, it works, but without options I need to set.

The issue seems to be simple but I don't know how to fix it, what am I doing wrong?

like image 557
Sergei Basharov Avatar asked Feb 01 '16 13:02

Sergei Basharov


2 Answers

https://www.npmjs.com/package/jsonwebtoken#jwtsignpayload-secretorprivatekey-options-callback

payload could be an object literal, buffer or string. Please note that exp is only set if the payload is an object literal.

like image 98
dcy Avatar answered Sep 18 '22 12:09

dcy


Set the payload as an object if you want to be able to set the expiresIn option

var token = jwt.sign(
    {id: accounts[request.headers.login].id}, // object and not string
    privateKey,
    {expiresIn: '60d'} // added days, default for ex 60 would be ms, you can also provide '1h' etc
)
like image 24
Pipo Avatar answered Sep 19 '22 12:09

Pipo