1.0.2 • Published 3 years ago

@nirangad/is-authenticated v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

is-authenticated

Simple middleware for Express to validate JSON web token

Install

npm i @nirangad/is-authenticated

Usage

Make sure to set SECRET_KEY environment variable which used to be passed to jwt.verify as jwt.Secret or jwt.GetPublicKeyOrSecret

Setting environment variable

# In .env file
SECRET_KEY="SECRET_KEY"

Preparing the JWT token

import jwt from "jsonwebtoken";

...

const token = jwt.sign(
  {
    id: user.id,
    email: user.email,
    timestamp: Date.now()
  },
  process.env.SECRET_KEY,
  {
    expiresIn: "10h"
  }
);

Using isAuthenticate in Express endpoint

import isAuthenticated from "@nirangad/is-authenticated";

# Express Routes
app.get("/product", isAuthenticated, (req, res) => {
  return res.json({ status: 1, message: "Welcome to Product Service" });
});
1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago