express-session-wrapper
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

express-session-wrapper

npm package Downloads

A package that extends the functionality of the express-session

Features

  • simplified api
  • promises support
  • absolute timeout

Install

npm i express-session-wrapper

Usage

import express from "express"
import { configureSessionWrapper } from "express-session-wrapper";

const PORT = 5500;

const app = express();

app.use(express.json());
app.use(
    session({
        // your config
    })
);
app.use(
    configureSessionWrapper({
        absoluteTimeoutInMilliseconds: 1 * 60 * 1000,
    })
);

app.get("/login", async (req, res) => {
    await req.sessionWrapper.createSession({userId:"userid"});
    res.send("Logged in successfully");
});

app.get("/getUserId", (req, res) => {
    const sessionData = req.sessionWrapper.getSessionDataIfExists();
    if(!sessionData){
        return res.status(401).send("Unauthorized")
    }
    res.send(sessionData.userId);
});

app.listen(PORT, async () => {
    console.log(`Listening on http://localhost:${PORT}`);
});

Typescript support

Create .d.ts file in your root directory and place the following code in it.

import "express-session-wrapper";

declare module "express-session-wrapper" {
  interface SessionWrapperData {
    //add your fields
  }
}

API

configureSessionWrapper

import { configureSessionWrapper } from "express-session-wrapper";
app.use(configureSessionWrapper(options))

Options

absoluteTimeoutInMilliseconds?

Default: undefined

Value Description
number Absolute timeout in milliseconds. See owasp
undefined Absolute timeout is disabled

req.sessionWrapper

createSession(sessionData)

Creates new session and saves given session data to the store. It also regenerates session id.
Returns empty promise

destroySession()

Destroys existing session
Returns empty promise

getSessionDataIfExists()

Returns saved session data
When session does not exist, it returns false

Package Sidebar

Install

npm i express-session-wrapper

Weekly Downloads

2

Version

1.1.0

License

MIT

Unpacked Size

14.6 kB

Total Files

19

Last publish

Collaborators

  • aadameqq