Socket
Socket
Sign inDemoInstall

iron-session

Package Overview
Dependencies
Maintainers
2
Versions
65
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

iron-session

Node.js stateless session utility using signed and encrypted cookies to store data. Works with Next.js, Express, NestJs, Fastify, and any Node.js HTTP framework.


Version published
Weekly downloads
154K
increased by17.07%
Maintainers
2
Weekly downloads
 
Created

What is iron-session?

The iron-session npm package is a lightweight and secure way to manage sessions in a Node.js application. It uses encryption to store session data in cookies, ensuring that the data is both secure and tamper-proof.

What are iron-session's main functionalities?

Create a session

This code demonstrates how to create a session and store user data in it. The session data is encrypted and stored in a cookie.

const { withIronSession } = require('iron-session/next');

const handler = async (req, res) => {
  req.session.set('user', { id: 1, admin: true });
  await req.session.save();
  res.send('Session created');
};

module.exports = withIronSession(handler, {
  password: process.env.SECRET_COOKIE_PASSWORD,
  cookieName: 'myapp_cookiename',
  cookieOptions: {
    secure: process.env.NODE_ENV === 'production',
  },
});

Retrieve session data

This code demonstrates how to retrieve session data. It checks if a user session exists and sends the user data in the response.

const { withIronSession } = require('iron-session/next');

const handler = async (req, res) => {
  const user = req.session.get('user');
  if (user) {
    res.send(`User ID: ${user.id}, Admin: ${user.admin}`);
  } else {
    res.send('No user session found');
  }
};

module.exports = withIronSession(handler, {
  password: process.env.SECRET_COOKIE_PASSWORD,
  cookieName: 'myapp_cookiename',
  cookieOptions: {
    secure: process.env.NODE_ENV === 'production',
  },
});

Destroy a session

This code demonstrates how to destroy a session. It removes the session data from the cookie.

const { withIronSession } = require('iron-session/next');

const handler = async (req, res) => {
  req.session.destroy();
  res.send('Session destroyed');
};

module.exports = withIronSession(handler, {
  password: process.env.SECRET_COOKIE_PASSWORD,
  cookieName: 'myapp_cookiename',
  cookieOptions: {
    secure: process.env.NODE_ENV === 'production',
  },
});

Other packages similar to iron-session

Keywords

FAQs

Package last updated on 21 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc