jwt-knex v0.0.15
jwt-knex
This library completely repeats the entire functionality of the library jsonwebtoken, with one important addition. jwt-knex allows you to store the token label in database with knex to verify validity. The absence of a token label in redis makes the token not valid. To destroy the token in jwt-knex, there is a destroy method. This makes it possible to make a token not valid until it expires.
The package is tested with oracledb but should be working just when passing an instance of knex in.
This package is inspired by jwt-redis
Installation
npm install jwt-knex
Support
This library is implemented as a part of other project and might contains bugs. Please create an issue on github, any contribution are welcomed.
Quick start
Create and run the migration script with knex to create the required table
knex migrate:make add_jwt_tables
Enter the following code to create the table. Make sure you add
JWT_ORACLE_TABLE_NAME
to your environment variables:exports.up = (knex) => knex.schema.createTable(process.env.JWT_ORACLE_TABLE_NAME, (table) => { table.string("key", 255).primary(); table.bigInteger("expiredAt").unsigned().defaultTo(Number.MAX_SAFE_INTEGER); }); exports.down = (knex) => knex.schema.dropTable(process.env.JWT_ORACLE_TABLE_NAME);
Run the migration script with knex.
knex migrate:latest
Added the jwt-knex package as in below:
const knex = require("knex")(knexConfig); const JwtKnex = require("jwt-knex"); const secretOrPrivateKey = "secret"; const jwt = new JwtKnex({ knex, secretOrPrivateKey, tableName: process.env.JWT_ORACLE_TABLE_NAME }); const payload = { sub: "1234567890", name: "John Doe", admin: true, jti: "jti", }; jwt .sign({ payload, expiresIn: "10h" }) .then(token => jwt.verify({ token })) .then(() => jwt.destroy({ jti: payload.jti }));
// TODO: documenting the APIs
Contribution
- Clone this repo
- Start the docker compose
docker-compose up
- Migrate the database
npm run knex -- migrate:latest
- Run tests
npm t