# als-session

> Flexible and secure session management library for Node.js using encrypted cookies.

Latest version **6.2.0** (published 2024-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install als-session
pnpm add als-session
yarn add als-session
bun add als-session
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 6.2.0 |
| Published | 2024-06-17 |
| First published | 2021-02-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 16.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alex Sorkin |
| Maintainers | alexsorkin |
| Keywords | session, session management, security, cookies, encrypted cookies, nodejs, express middleware, http middleware, session middleware, web development |

## Links

- npm: https://www.npmjs.com/package/als-session
- npm.io page: https://npm.io/package/als-session

## Dependencies (1)

- [als-cookie](https://npm.io/package/als-cookie.md) ^3.0.0

## Alternatives

- [@sindresorhus/slugify](https://npm.io/package/@sindresorhus/slugify.md) — 3.7M weekly downloads
- [solid-js](https://npm.io/package/solid-js.md) — 2.7M weekly downloads
- [expo-glass-effect](https://npm.io/package/expo-glass-effect.md) — 2.5M weekly downloads
- [nanoassert](https://npm.io/package/nanoassert.md) — 780.8K weekly downloads
- [@ffmpeg/ffmpeg](https://npm.io/package/@ffmpeg/ffmpeg.md) — 529.5K weekly downloads

## Recent versions

- 6.2.0 (latest) — 2024-06-17
- 6.1.1 — 2024-06-05
- 6.1.0 — 2024-06-05
- 6.0.0 — 2024-05-20
- 5.0.0 — 2024-05-16
- 4.5.0 — 2024-05-01
- 4.1.4 — 2023-12-25
- 4.1.3 — 2023-12-23
- 4.1.2 — 2023-12-23
- 4.1.1 — 2023-12-23
- 4.1.0 — 2023-12-20
- 4.0.0 — 2023-12-16
- 3.0.2 — 2023-11-08
- 3.0.1 — 2023-11-08
- 3.0.0 — 2023-11-03
- … 15 more at https://npm.io/package/als-session/versions

## README

# als-session

## Description

`als-session` is a powerful and flexible library for managing sessions in Node.js applications. It provides an easy way to store and manage sessions using encrypted cookies. 

**Advantages of using `als-session`:**
- Manages client-side sessions using cookies.
- Automatically encrypts session data to enhance security.
- Offers flexibility in configuring session parameters including lifespan, access methods, and more.

## Change log
* req.sessionCookieOptions added

## Installation

Install `als-session` using npm or yarn:

```bash
npm install als-session
```

## Quick Start

### Using with Express

```javascript
const express = require('express');
const sessionMw = require('als-session');

const app = express();

app.use(sessionMw());

app.get('/', (req, res) => {
   req.session.visits = (req.session.visits || 0) + 1;
   res.send(`Number of visits: ${req.session.visits}`);
});

app.listen(3000, () => console.log('App running on port 3000'));
```

### Using with an HTTP Server

```javascript
const http = require('http');
const sessionMw = require('als-session')();

const server = http.createServer((req, res) => {
   sessionMw(req, res, () => {
      if (req.url === '/') {
         req.session.visits = (req.session.visits || 0) + 1;
         res.end(`Number of visits: ${req.session.visits}`);
      }
   });
});

server.listen(3000, () => console.log('Server running on port 3000'));
```

**Available session objects:**
- `req.session`: a proxy object for handling session data.
  - You can delete, create and update properties
  - You can't reasign the session (req.session = {} will throw exception)
- `req.destroySession()`: a function to delete all session data.
- `req.sessionCookieOptions`: object with cookie options for this response
  - includes defaultoptions
  - Can be changed (for example changing maxAge for temporal sessions)

## How It Works

Sessions in `als-session` are stored in encrypted cookies on the client side. Every change in session data automatically updates the cookie and also sets the timestamp of the last update. The cookie's lifespan is controlled both on the client (through `Max-Age` and `Expires`) and on the server.

## Advanced Usage

### Configuration Parameters

- `maxAge` (default: 2592000 seconds) - The lifespan of the cookie in seconds.
- `logger` (default: `console.log`) - A function for logging errors.
- `methods` (default: `['GET', 'PUT', 'POST', 'PATCH', 'DELETE']`) - HTTP methods for which the session will be activated.
- `name` (default: 'session') - The name of the session cookie.
- `sameSite` (default: 'lax') - The SameSite attribute for the cookie that helps guard against CSRF attacks. Can be 'lax', 'none', 'strict'.
- `prefix` (String, optional) - prefix for encryption
- `cryptOptions` (Object, optional) - options for encryption
  - more information here: [als-crypt](https://www.npmjs.com/package/als-crypt)

**Example with custom settings:**

```javascript
const sessionConfig = {
   maxAge: 86400,
   logger: message => console.error(message),
   methods: ['GET', 'POST'],
   name: 'mySession',
   sameSite: 'strict'
};

const app = express();
app.use(sessionMiddleware(sessionConfig));
```

---
_Source: https://npm.io/package/als-session · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
