npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

auth-middleware-jwt

v1.0.17

Published

This package will be used for jsonwebtoken's middleware where the token generation and verification will be done by importing two objects from this package

Downloads

9

Readme

auth-middleware-jwt

npm

npm GitHub

JWT authentication middleware for any nodejs API. Just install and import two objects from package as shown below.

Install using npm

npm i auth-middleware-jwt jsonwebtoken

Or using yarn

yarn add auth-middleware-jwt jsonwebtoken

Make a .env file of your project root and set two environment variables as shown below

ACCESS_TOKEN_EXPIRES_IN=1h //modify as you need eg: 60s, 1m, 1h, 7d
REFRESH_TOKEN_EXPIRES_IN=1y //modify as you need eg: 30d, 7d, 24h
ACCESS_TOKEN_SECRET_KEY=my_secret_key //use your secret key
REFRESH_TOKEN_SECRET_KEY=my_secret_key //use your secret key

To have this a try copy this template and run your node application

const express = require('express');
const dotenv = require('dotenv');
const {
    getAccessToken,
    getRefreshToken,
    RefreshTokenValidation,
    AccessTokenValidation,
} = require('auth-middleware-jwt');
const app = express();
const port = 3010 || process.env.PORT;
dotenv.config();

app.use(express.json());

// use redis to store the refresh tokens;
let refreshTokens = [];
// use your database to store the user data
let user = {};

//@Description: Route for a login auth route with user credentials
//Route: http://localhost:3000
//Method: POST
//Set Login credentials in body as shown below
// {
//     name:'useer',
//     email:'[email protected]',
//     password:'password'
// }
app.post('/', async (req, res) => {
    const { name, email, password } = req.body;

    //setup your authentication checking logics
    //get your user data to send as payload of JWT
    user.id = '49afbf2a-0c08-4636-963c-1933507fb168';
    user.name = name;
    user.email = email;
    user.image = 'https://picsum.photos/100/100';
    try {
        let accessToken = await getAccessToken(user);
        let refreshToken = await getRefreshToken({ user: user.id });

        refreshTokens.push(refreshToken);

        res.json({
            code: 200,
            isSuccess: true,
            status: 'success',
            data: {
                accessToken,
                refreshToken,
            },
        });
    } catch (error) {
        console.log(error);
        res.json({
            code: 200,
            isSuccess: true,
            status: 'success',
            message: error.message,
        });
    }
});

//@Description: Set the access token as bearer token in header as key 'Authorization' and value 'Bearer <the token will be given after login />'
//Route: http://localhost:3000/protected_route
//Method: POST
app.post('/protected_route', AccessTokenValidation, async (req, res) => {
    if (req.user) {
        res.json({
            code: 200,
            isSuccess: true,
            status: 'success',
            data: {
                user: req.user,
            },
        });
    } else {
        res.json({
            code: 404,
            isSuccess: false,
            status: 'failed',
            message: 'User is not found',
        });
    }
});

/*
@Description: If the the access token is expired then use this route to renew access and refresh token by sending the refresh token in header as refreshToken and value 'Bearer <the refresh token will be given after login or registration />'
Route: http:localhost:3000/get-access-token
Method: POST
*/
app.post('/get-access-token', RefreshTokenValidation, async (req, res) => {
    let { id, token } = req.user;

    if (refreshTokens.includes(token)) {
        try {
            let accessToken = await getAccessToken(id);
            let refreshToken = await getAccessToken({ user: id });

            refreshTokens[id] = refreshToken;

            res.json({
                code: 200,
                status: 'success',
                isSuccess: true,
                data: {
                    isLoggedIn: true,
                    accessToken,
                    refreshToken,
                },
            });
        } catch {
            res.status(403).json({
                code: 403,
                isSuccess: false,
                status: 'failed',
                message: 'Request not allowed !',
            });
        }
    } else {
        res.status(403).json({
            code: 403,
            isSuccess: false,
            status: 'failed',
            message: 'Request not allowed !',
        });
    }
});

app.listen(port, () => console.log(`App is listening on ${port}`));