Giter VIP home page Giter VIP logo

sjwt's Introduction

SJWT

A simple Json Web Token module written with minimal footprint in mind. SJWT uses SHA256 for signatures. Injecting custom hash functions is also supported.

Installation

$ npm install -S '@paweljarema/sjwt'

Setup

To create secure tokens, you need to pass a secret to SJWT constructor. It's best to prepare an instance in a separate file and require later:

jwt.js
const SJWT = require('@paweljarema/sjwt')
const { appSecret } = require('../config/keys')   // appSecret is just a string

module.exports = new SJWT({ secret: appSecret })

Usage

To create a secure token, do:

const jwt = require('jwt.js')   // jwt.js from previous step
const token = jwt.tokenize(dataObject)

To parse token:

jwt.parse(token)   // returns 'undefined' for invalid tokens

Token expiry date

If you want your tokens to expire with time, you need to implement this yourself:

const TTL = 24 * 60 * 60 * 1000
const data = {
    _id: 'session_id or user_id',
    expires: Date.now() + TTL,
}
const token = jwt.tokenize(data)

To test if token expired:

    const data = jwt.parse(token)
    const tokenIsValid = data && data.expires >= Date.now()

Custom hash function

To use customgit hashing function, provide additional props in constructor, just like we did in setup. Hash function should take a string and return a hashed string:

const SJWT = require('@paweljarema/sjwt')
const { appSecret } = require('../config/keys')
const myHashFunction = require('my-hash-function')
module.exports = new SJWT({
    secret: appSecret,
    hashFunction: myHashFunction,
    hashFunctionName: 'my-hash-function'
})

License

Use for good as much as you like :)

sjwt's People

Contributors

paweljarema avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.