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

session-jwt

v2.0.6

Published

Session-jwt is nodejs module that allows you to use sessions with jwt and refresh token.

Downloads

9

Readme

Session-jwt Build Status Codacy Badge

This library helps you manage user sessions via jwt.

How does it work?

The library generates two tokens (jwt and refreshToken).
The jwt is a token with a close expiry date.
The refreshToken does not expire, it is used to get a new jwt when it expires.

Installation

npm install session-jwt

Settings

const session = require('session-jwt');

session.settings(process.env.YOUR_SECRET_ENV_VAR);

You must also add cookie-parser if you want the library to automatically obtain/add tokens to express requests/responses (more on this later).

const session = require('session-jwt');
const cookieParser = require("cookie-parser");

session.settings(process.env.YOUR_SECRET_ENV_VAR);

Optional Settings

As you saw from the code above you need to call the settings() method to set the jwt secret, but you can set other things like jwtHeaderKeyName (jwt position in header)

jwtHeaderKeyName is by default "jwt"

Usage

Create session

With cookie

When you want to create new session you can use the newSessionInCookies method

app.get("/login", async(req, res) => {
    let { jwt, refreshToken } = await session.newSessionInCookies({ "user": "ale" }, res, "user");

    res.send({ jwt });
});

The first parameter is an object of data you want to save in token. The second parameter is the Express response object, this is used to set the refreshToken cookie

This method returns the jwt and refreshToken through promise

Without cookie

If you don't want to use cookie you can use the newSession method

app.get("/login", async(req, res) => {
    let { jwt, refreshToken } = await session.newSession({ "user": "ale" }, "user");

    res.status(200).json({ jwt, refreshToken });
});

In this case nothing is saved in cookie

ensureAuth

To be sure a user has a valid jwt to access an endpoint you must use ensureAuth in your Express router.

app.get("/user", session.ensureAuth, (req, res) => {ù
	//there is a valid jwt
    //You can access req.session to get data saved in jwt
    res.send("kk");
});

Doing so will automatically block all requests that do not have a valid jwt. Remember that the jwt must be in the position of the header described in the settings

You can access req.session to get data saved in jwt*

Refresh token

When the jwt expires you have to create a new one through the refreshToken

With cookie

If your refreshToken is in cookies named refresh, or if you created the session using the newSessionInCookies method you can use this method

app.get("/refresh", async(req, res) => {
    try {
        const jwt = await session.refreshFromCookie(req);
        res.status(200).json({ jwt });
    } catch (err) {
    	//if refreshToken is invalid, the user must log in
        res.redirect(301, "/login");
        res.end();
    }
});

This method returns a valid jwt or false (if refreshCookie is invalid)

Without cookie

If you are managing the refreshCookie on your own you must use this method

app.get("/refresh", async(req, res) => {
    try {
        const refreshToken = req.body.refreshToken;
        const jwt = await session.refresh(refreshToken);
        res.status(200).json({ jwt });
    } catch (err) {
        //if refreshToken is invalid, the user must log in
        res.redirect(301, "/login");
        res.end();
    }
});