|
| 1 | +# Session middleware for Hono |
| 2 | + |
| 3 | +[](https://codecov.io/github/honojs/middleware) |
| 4 | + |
| 5 | +Session middleware for Hono using encrypted JSON Web Tokens. |
| 6 | + |
| 7 | +This middleware depends on the following pacakges: |
| 8 | + |
| 9 | +- [`@panva/hkdf`](https://github.com/panva/hkdf) |
| 10 | +- [`jose`](https://github.com/panva/jose) |
| 11 | + |
| 12 | +Other resources worth reading include: |
| 13 | + |
| 14 | +- [The Copenhagen Book](https://thecopenhagenbook.com/) by [Pilcrow](https://github.com/pilcrowOnPaper) |
| 15 | +- [Session Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html) from [OWASP](https://cheatsheetseries.owasp.org/index.html) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```sh |
| 20 | +npm i @hono/session |
| 21 | +``` |
| 22 | + |
| 23 | +## Environment Variables |
| 24 | + |
| 25 | +```sh |
| 26 | +AUTH_SECRET= |
| 27 | +``` |
| 28 | + |
| 29 | +> [!TIP] |
| 30 | +> Quickly generate a good secret with `openssl` |
| 31 | +> |
| 32 | +> ```sh |
| 33 | +> $ openssl rand -base64 32 |
| 34 | +> ``` |
| 35 | +
|
| 36 | +## Options |
| 37 | +
|
| 38 | +| Option | Type | Description | |
| 39 | +| ---------------- | ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | |
| 40 | +| `generateId`? | `() => string` | Function to generate a unique session ID | |
| 41 | +| `secret`? | `string` or [`EncryptionKey`](#EncryptionKey) | 32-byte, hex-encoded string, or encryption key, used to encrypt the session cookie. Defaults to `process.env.AUTH_SECRET` | |
| 42 | +| `sessionCookie`? | [`SessionCookieOptions`](#SessionCookieOptions) | Session cookie options | |
| 43 | +
|
| 44 | +## `EncryptionKey` |
| 45 | +
|
| 46 | +- [`jose.CryptoKey`](https://github.com/panva/jose/blob/main/docs/types/type-aliases/CryptoKey.md) | [`jose.KeyObject`](https://github.com/panva/jose/blob/main/docs/types/interfaces/KeyObject.md) | [`jose.JWK`](https://github.com/panva/jose/blob/main/docs/types/interfaces/JWK.md) | [`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) |
| 47 | +
|
| 48 | +## `SessionCookieOptions` |
| 49 | +
|
| 50 | +> [!IMPORTANT] |
| 51 | +> By default, session cookies do not expire. |
| 52 | +> It is recommended to provide value for `duration.absolute` |
| 53 | +
|
| 54 | +### Properties |
| 55 | +
|
| 56 | +| Property | Type | Description | |
| 57 | +| ----------- | --------------------------------------------------------------- | --------------------------------------------------------------------------------- | |
| 58 | +| `duration`? | [`MaxAgeDuration`](#MaxAgeDuration) | The maximum age duration of the session cookie. By default, no maximum age is set | |
| 59 | +| `name`? | `string` | The name of the session cookie. Defaults to `sid` | |
| 60 | +| `options`? | [`CookieOptions`](https://hono.dev/docs/helpers/cookie#options) | Session cookie options | |
| 61 | +
|
| 62 | +## `MaxAgeDuration` |
| 63 | +
|
| 64 | +See [Session lifetime](https://thecopenhagenbook.com/sessions#session-lifetime) |
| 65 | +
|
| 66 | +### Properties |
| 67 | +
|
| 68 | +| Property | Type | Description | |
| 69 | +| ------------- | -------- | ---------------------------------------------------------------------------------------------------------------- | |
| 70 | +| `absolute` | `number` | Duration in seconds a session will be valid for, after which it will be expired and have to be re-authenticated. | |
| 71 | +| `inactivity`? | `number` | Duration in seconds a session will be considered active, during which the session max age can be extended. | |
| 72 | +
|
| 73 | +## Example |
| 74 | +
|
| 75 | +```ts |
| 76 | +import { session } from '@hono/session' |
| 77 | +import { Hono } from 'hono' |
| 78 | +
|
| 79 | +const app = new Hono() |
| 80 | +
|
| 81 | +app.use(session()).get('/', async (c) => { |
| 82 | + const data = await c.var.session.get() |
| 83 | + return c.json(data) |
| 84 | +}) |
| 85 | +
|
| 86 | +export default app |
| 87 | +``` |
| 88 | +
|
| 89 | +### With Session storage |
| 90 | +
|
| 91 | +```ts |
| 92 | +import { session, sessionStorage } from '@hono/session' |
| 93 | +import { Hono } from 'hono' |
| 94 | +
|
| 95 | +const app = new Hono() |
| 96 | +
|
| 97 | +app |
| 98 | + .use( |
| 99 | + sessionStorage({ |
| 100 | + delete(sid) {}, |
| 101 | + async get(sid) {}, |
| 102 | + set(sid, value) {}, |
| 103 | + }), |
| 104 | + session() |
| 105 | + ) |
| 106 | + .get('/', async (c) => { |
| 107 | + const data = await c.var.session.get() |
| 108 | + return c.json(data) |
| 109 | + }) |
| 110 | +
|
| 111 | +export default app |
| 112 | +``` |
| 113 | +
|
| 114 | +See also: |
| 115 | +
|
| 116 | +- [Cloudflare KV as session storage](./examples/cloudflare-kv.ts) |
| 117 | +- [Using Unstorage as session storage](./examples/unstorage.ts) |
| 118 | +
|
| 119 | +## Author |
| 120 | +
|
| 121 | +Jonathan haines <https://github.com/barrythepenguin> |
| 122 | +
|
| 123 | +## License |
| 124 | +
|
| 125 | +MIT |
0 commit comments