# cookie-session-store

> A cookie based session store

Latest version **1.0.2** (published 2021-07-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install cookie-session-store
pnpm add cookie-session-store
yarn add cookie-session-store
bun add cookie-session-store
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2021-07-20 |
| First published | 2021-07-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 15.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | András Palásti |
| Maintainers | andraspalasti |

## Links

- npm: https://www.npmjs.com/package/cookie-session-store
- Repository: https://github.com/andraspalasti/cookie-session-store
- Homepage: https://github.com/andraspalasti/cookie-session-store#readme
- Issues: https://github.com/andraspalasti/cookie-session-store/issues
- npm.io page: https://npm.io/package/cookie-session-store

## Dependencies (2)

- [cookie](https://npm.io/package/cookie.md) ^0.4.1
- [crypto](https://npm.io/package/crypto.md) ^1.0.1

## Recent versions

- 1.0.2 (latest) — 2021-07-20
- 1.0.1 — 2021-07-19
- 1.0.0 — 2021-07-18

## README

# cookie-session-store

This package is created to manage cookie sessions. (It is a Remix like createCookieSessionStorage api)
The package can handle any request type, because it uses the request's cookie header as a parameter.

To install run:
```
npm install cookie-session-store
```

**Example:**
```javascript
import { createCookieSessionStore } from "cookie-session-store";

// this should be in a separate file
// perhaps session.js/ts
const { getSession, destroySession, commitSession} = createCookieSessionStore({ 
	name: "__session", 
	secret: "this is a secret",
	cookieOptions: {...}
})

app.get((req, res) => {
	const session = getSession(req.headers.cookie);
	const userId = session.get("userId");

	if (userId) {
		session.set("userId", 5)
		// you need to set this if you modify the session
		// modification is when you call set/unset
		res.set("Set-Cookie", commitSession(session));
	} else {
		res.set("Set-Cookie", destroySession())
	}
})
```

### API

- `getSession(httpCookieHeader: string)` it reads the cookie header and gets the cookie by the name that you specified in `createCookieSessionStore` than it decrypts the cookie with the secret than it returns a session object

- `commitSession(session: Session)` it gets a session object as a parameter, than it encrypts that session object and returns a proper set cookie header this way we persist changes made to the session

- `destroySession()` it returns a set cookie header that destroys the session cookie

- `session.get(key: string)` 
returns the value by the key if key is not in the session it returns undefined

- `session.set(key: string, value: string | number | boolean | null)` if the key is in the session than it's value will be overridden with the value parameter if the key is not in the session than the key value pare will be added to it

- `session.has(key: string)` checks if a given key is in the session returns true or false

- `session.unset(key: string)` deletes the key value pair from the session

- `session.data()` returns the raw session object

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