truesign-fastify-hook
v2.0.0
Published
Simple Fastify hook for handling requests with truesign tokens on query string
Downloads
120
Maintainers
Readme
This is a simple Fastify hook for handling requests with Truesign tokens on query string.
Usage example
import { getTruesignHook, TruesignHookConfig } from 'truesign-fastify-hook';
function shouldAcceptToken(
decryptedToken: DecryptedToken,
config: TruesignHookConfig
): boolean {
if (((Date.now() - decryptedToken.timestamp) / 1000) > config.tokenExpirationTimeSeconds) return false;
return true;
}
const trueSignOptions: TruesignHookConfig = {
shouldAcceptToken,
encryptionKey: process.env.TRUESIGN_KEY,
allowUnauthenticated: false,
// we can add more configuration so we have this available in shouldAcceptToekn function
tokenExpirationTimeSeconds: 30,
};
// when dealing with routes
fastify.addHook('onRequest', getTruesignHook(trueSignOptions));
// or in each secured route
fastify.route({
method: 'GET',
url: '/',
schema: { ... },
onRequest: getTruesignHook(trueSignOptions),
// or
preValidation: getTruesignHook(trueSignOptions),
// or others
});
The decrypted token interface is a copy from Truesign docs.
This is a simple package for a simple use case. If you want to extend it to more use cases, plese contribute!
Install
npm install
Run tests
npm run test
Author
👤 Julian Toledo
- Twitter: @julipan37
- Github: @julianitor
Show your support
Give a ⭐️ if this project helped you!
This README was generated with ❤️ by readme-md-generator