How can I change the JWT expire time in FeathersJS?

Viewed 1561

I was looking at this question on how to set the sub claim of a JWT in FeathersJS, but when I print hook.params, there is no jwt in there. Only authenticated, query, route, provider, headers, user and payload.

So I remain with the question: how can I change the expire time of a JWT token in Feathers?

2 Answers

Found it :) I took a look at the code linked in the post (link has changed, but found it back easily when browsing the git repo) and saw that in params, you just need to create your own jwt object and these options will be merged when creating the JWT.

So, if anyone else stumbles upon this, here is my code:

app.service('authentication').hooks({
  before: {
    create: [
      authentication.hooks.authenticate(config.strategies),

      context => {
        context.params.jwt = { expiresIn: 10 }; // 10 seconds
      }
    ],
    remove: [
      authentication.hooks.authenticate('jwt')
    ]
  }
});

For posterity,

you can easily change that in config/default.json:

{
  // ...
  "authentication": {
    "jwtOptions": {
        "expiresIn": "2 days" // Or "10h" or just a number which is interpreted as seconds
    }
  }
}
Related