1.0.2 • Published 3 years ago
@nirangad/is-authenticated v1.0.2
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" });
});