Skip to content

JSON WEB TOKEN

siddharthakonakanchi edited this page Dec 5, 2017 · 2 revisions

Packages and API used

  1. Description
  2. Installation
  3. API calls using package
  4. Code Snippet
  5. Database Insertion
  6. Reference

JSON Web Token

Description

JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA

Installation

npm install jsonwebtoken

Usage in our project

In our application once the user is logged in, each subsequent request to a page will internally call the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

We are using secretOrPrivateKey a string, buffer, or object containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECD We are setting timeout of secretOrPrivateKey parameter to 1 hour.

API Calls using package

/users/login To set the secretOrPrivateKey and timeout to 60 minutes

Code Snippet

/users/login

                    console.log(user);
                    var token = jwt.sign({ data: user }, 'mysecretkey', { expiresIn: tokenExpireTime });
                    user.password = '';
                    res.status(200);
                    return res.json({
                        success: true,
                        message: 'Login successful',
                        token: token,
                        data: user
                    });

Database Insertion

No Database Involved

Reference

More information can be obtained from https://www.npmjs.com/package/jsonwebtoken

Clone this wiki locally