ForrestJS service which helps handling jwt activities.
npm install @forrestjs/service-jwtForrestJS service which helps handling jwt activities. More or less it provides
a _Promise_ wrapped jsonwebtoken module.
service-jwt decorates the _App's Context_ with a jwt.sign() and jwt.verify() helpers:
``js
registerAction({
target: 'myHook',
handler: async (args, ctx) => {
const token = await ctx.jwt.sign({ foo: 123 });
console.log(token);
const data = await ctx.jwt.verify(token);
console.log(data);
},
});
`
After the App initializes, you can simply import the helpers and use it straight:
`js
const { sign, verify } = require('@forrestjs/service-jwt');
const token = await sign({ foo: 123 });
const isValid = await verify(token);
`
NOTE: it is important to list service-jwt _AFTER_ service-env so that any kind of environment
configuration is available to be used.
`bash`
JWT_SECRET=xxx
JWT_DURATION=1y
`js`
registerAction({
target: '$SETTINGS',
handler: ({ setConfig }) => {
setConfig('jwt.secret', 'your-safe-secret');
setConfig('jwt.duration', '1y');
},
});
settings can use whatever you would pass to jsonwebtoken. The propertyexpiresIn is defaulted to the global duration setting.
customSecret is defaulted to the global secret setting.
customSecret is defaulted to the global secret` setting.
It resolves with the token's decoded content or throws an error in case it it
not valid.