Git Product home page Git Product logo

easy-jwt's Introduction

json logo

Easy JWT

A lightweght JSON Web Token library for node.js

Installation

npm install easy-jwt

Usage

var jwt = require('easy-jwt')

JWT Sign

jwt.sign(payload, secret_key, [options])

Payload & Secret

payload could be an object literal, buffer or string representing valid JSON.

secret is a string, buffer or an object containing the secret or encoded private key.

Options

alg algorithm used for encryption. Supported algorithms: HS256 HS384 HS512

expireDate expiry date of JSON Web Token in ms. Leave blank for no expiration.

// Sign using SHA-256 with 1 hour expiry
var secret = fs.readFileSync('secret.key')
jwt.sign({ foo: 'bar' }, secret, { alg: 'HS256', expireDate: Date.now() + 3600000 })

JWT Verify

jwt.verify(token, secret_key, [options])

Payload & Secret

payload JSON Web Token.

secret is a string, buffer or an object containing the secret or encoded private key.

Options

maxAge maximum age tokens are allowed to be valid after expiry. Set to 0 by default.

ignoreExpiration ignore expired token errors. False by default.

complete return header and payload in one JSON object. False by default.

// Verify JSON Web Token and return header with payload
var secret = fs.readFileSync('secret.key')
jwt.verify(token, secret, { complete: true })

Error Handling

try {
    jwt.verify(token, secret, { complete: true })
} catch (error) {
    /*
        Error: {
            "name": "TokenError",
            "message": "token expired"
        }
    */
}

SignError

This error shows up during the signing process of a JSON Web Token

Message

payload is required payload was not specified when jwt.sign was called.

invalid algorithm the algorithm you are trying to use is not supported, or does not exist.

secret is required no secret or key was specified when jwt.sign was called.

TokenError

This error shows up during the verification of a JSON Web Token

Message

token expired the JSON Web Token expired.

invalid token signature the signature of the token failed to pass verification.

incorrect token format the token specified was not using a proper JSON Web Token format.

invalid algorithm the header of the JSON Web Token contained an unsupported algorithm format.

secret is required no secret or key was specified when jwt.verify was called.

easy-jwt's People

Watchers

Ian avatar

Forkers

ihmpavel

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.