@remix-run/session-middleware 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -18
- package/dist/lib/session.d.ts +5 -3
- package/dist/lib/session.d.ts.map +1 -1
- package/dist/lib/session.js +1 -1
- package/package.json +11 -10
- package/src/lib/session.ts +2 -4
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# session-middleware
|
|
2
2
|
|
|
3
|
-
Session middleware for Remix using signed cookies. It loads session state from incoming requests,
|
|
3
|
+
Session middleware for Remix using signed cookies. It loads session state from incoming requests, exposes it as `context.session` (or `context.get(Session)`), and persists updates automatically.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
7
7
|
- **Session Lifecycle Handling** - Reads and saves session state per request
|
|
8
|
-
- **Context Integration** - Exposes session
|
|
8
|
+
- **Context Integration** - Exposes `context.session` (or `context.get(Session)`)
|
|
9
9
|
- **Secure Cookie Support** - Designed for signed session cookies
|
|
10
10
|
|
|
11
11
|
## Installation
|
|
@@ -17,11 +17,10 @@ npm i remix
|
|
|
17
17
|
## Usage
|
|
18
18
|
|
|
19
19
|
```ts
|
|
20
|
-
import { createRouter } from 'remix/
|
|
20
|
+
import { createRouter } from 'remix/router'
|
|
21
21
|
import { createCookie } from 'remix/cookie'
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import { session } from 'remix/session-middleware'
|
|
22
|
+
import { createCookieSessionStorage } from 'remix/session-storage/cookie'
|
|
23
|
+
import { session } from 'remix/middleware/session'
|
|
25
24
|
|
|
26
25
|
let sessionCookie = createCookie('__session', {
|
|
27
26
|
secrets: ['s3cr3t'], // session cookies must be signed!
|
|
@@ -37,18 +36,19 @@ let router = createRouter({
|
|
|
37
36
|
})
|
|
38
37
|
|
|
39
38
|
router.get('/', (context) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return new Response(`Count: ${session.get('count')}`)
|
|
39
|
+
context.session.set('count', Number(context.session.get('count') ?? 0) + 1)
|
|
40
|
+
return new Response(`Count: ${context.session.get('count')}`)
|
|
43
41
|
})
|
|
44
42
|
```
|
|
45
43
|
|
|
46
44
|
The middleware:
|
|
47
45
|
|
|
48
46
|
- Reads the session from the cookie on incoming requests
|
|
49
|
-
- Makes it available as `context.get(Session)`
|
|
47
|
+
- Makes it available as `context.session` (or `context.get(Session)`)
|
|
50
48
|
- Automatically saves session changes and sets the cookie on responses
|
|
51
49
|
|
|
50
|
+
Use `context.session` (or `context.get(Session)`) for normal session reads and writes.
|
|
51
|
+
|
|
52
52
|
Note: The session cookie must be signed for security. This prevents tampering with the session data on the client.
|
|
53
53
|
|
|
54
54
|
### Login/Logout Flow
|
|
@@ -56,11 +56,9 @@ Note: The session cookie must be signed for security. This prevents tampering wi
|
|
|
56
56
|
A basic login/logout flow could look like this:
|
|
57
57
|
|
|
58
58
|
```ts
|
|
59
|
-
import * as res from 'remix/
|
|
60
|
-
import { Session } from 'remix/session'
|
|
59
|
+
import * as res from 'remix/router/response-helpers'
|
|
61
60
|
|
|
62
|
-
router.get('/login', ({
|
|
63
|
-
let session = get(Session)
|
|
61
|
+
router.get('/login', ({ session }) => {
|
|
64
62
|
let error = session.get('error')
|
|
65
63
|
return res.html(`
|
|
66
64
|
<html>
|
|
@@ -77,8 +75,7 @@ router.get('/login', ({ get }) => {
|
|
|
77
75
|
`)
|
|
78
76
|
})
|
|
79
77
|
|
|
80
|
-
router.post('/login', ({ get }) => {
|
|
81
|
-
let session = get(Session)
|
|
78
|
+
router.post('/login', ({ get, session }) => {
|
|
82
79
|
let formData = get(FormData)
|
|
83
80
|
let username = formData.get('username')
|
|
84
81
|
let password = formData.get('password')
|
|
@@ -95,8 +92,7 @@ router.post('/login', ({ get }) => {
|
|
|
95
92
|
return res.redirect('/dashboard')
|
|
96
93
|
})
|
|
97
94
|
|
|
98
|
-
router.post('/logout', ({
|
|
99
|
-
let session = get(Session)
|
|
95
|
+
router.post('/logout', ({ session }) => {
|
|
100
96
|
session.destroy()
|
|
101
97
|
return res.redirect('/')
|
|
102
98
|
})
|
package/dist/lib/session.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { Cookie } from '@remix-run/cookie';
|
|
2
2
|
import type { Middleware } from '@remix-run/fetch-router';
|
|
3
3
|
import { Session, type SessionStorage } from '@remix-run/session';
|
|
4
|
-
type SetSessionContextTransform = readonly [readonly [typeof Session, Session]];
|
|
5
4
|
/**
|
|
6
5
|
* Middleware that manages request session state on request context.
|
|
7
6
|
*
|
|
@@ -9,6 +8,9 @@ type SetSessionContextTransform = readonly [readonly [typeof Session, Session]];
|
|
|
9
8
|
* @param sessionStorage The storage backend for session data
|
|
10
9
|
* @returns The session middleware
|
|
11
10
|
*/
|
|
12
|
-
export declare function session(sessionCookie: Cookie, sessionStorage: SessionStorage): Middleware<
|
|
13
|
-
|
|
11
|
+
export declare function session(sessionCookie: Cookie, sessionStorage: SessionStorage): Middleware<{
|
|
12
|
+
key: typeof Session;
|
|
13
|
+
value: Session;
|
|
14
|
+
property: 'session';
|
|
15
|
+
}>;
|
|
14
16
|
//# sourceMappingURL=session.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/lib/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEjE
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/lib/session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAA;AAC/C,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AACzD,OAAO,EAAE,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEjE;;;;;;GAMG;AACH,wBAAgB,OAAO,CACrB,aAAa,EAAE,MAAM,EACrB,cAAc,EAAE,cAAc,GAC7B,UAAU,CAAC;IAAE,GAAG,EAAE,OAAO,OAAO,CAAC;IAAC,KAAK,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,SAAS,CAAA;CAAE,CAAC,CA8B1E"}
|
package/dist/lib/session.js
CHANGED
|
@@ -16,7 +16,7 @@ export function session(sessionCookie, sessionStorage) {
|
|
|
16
16
|
}
|
|
17
17
|
let cookieValue = await sessionCookie.parse(context.headers.get('Cookie'));
|
|
18
18
|
let session = await sessionStorage.read(cookieValue);
|
|
19
|
-
context.set(Session, session);
|
|
19
|
+
context.set(Session, session, { property: 'session' });
|
|
20
20
|
let response = await next();
|
|
21
21
|
if (session !== context.get(Session)) {
|
|
22
22
|
throw new Error('Cannot save session that was initialized by another middleware/handler');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix-run/session-middleware",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Middleware for managing sessions with cookie-based storage",
|
|
5
5
|
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,17 +28,17 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^24.6.0",
|
|
30
30
|
"@typescript/native-preview": "7.0.0-dev.20251125.1",
|
|
31
|
-
"@remix-run/assert": "0.
|
|
32
|
-
"@remix-run/cookie": "0.5.
|
|
33
|
-
"@remix-run/
|
|
34
|
-
"@remix-run/
|
|
35
|
-
"@remix-run/
|
|
36
|
-
"@remix-run/
|
|
31
|
+
"@remix-run/assert": "0.2.0",
|
|
32
|
+
"@remix-run/cookie": "0.5.2",
|
|
33
|
+
"@remix-run/fetch-router": "0.19.0",
|
|
34
|
+
"@remix-run/test": "0.4.0",
|
|
35
|
+
"@remix-run/headers": "0.20.0",
|
|
36
|
+
"@remix-run/session": "0.4.1"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@remix-run/cookie": "^0.5.
|
|
40
|
-
"@remix-run/
|
|
41
|
-
"@remix-run/
|
|
39
|
+
"@remix-run/cookie": "^0.5.2",
|
|
40
|
+
"@remix-run/fetch-router": "^0.19.0",
|
|
41
|
+
"@remix-run/session": "^0.4.1"
|
|
42
42
|
},
|
|
43
43
|
"keywords": [
|
|
44
44
|
"fetch",
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"build": "tsgo -p tsconfig.build.json",
|
|
53
53
|
"clean": "git clean -fdX",
|
|
54
54
|
"test": "remix-test",
|
|
55
|
+
"test:bun": "bun x --bun remix-test",
|
|
55
56
|
"typecheck": "tsgo --noEmit"
|
|
56
57
|
}
|
|
57
58
|
}
|
package/src/lib/session.ts
CHANGED
|
@@ -2,8 +2,6 @@ import type { Cookie } from '@remix-run/cookie'
|
|
|
2
2
|
import type { Middleware } from '@remix-run/fetch-router'
|
|
3
3
|
import { Session, type SessionStorage } from '@remix-run/session'
|
|
4
4
|
|
|
5
|
-
type SetSessionContextTransform = readonly [readonly [typeof Session, Session]]
|
|
6
|
-
|
|
7
5
|
/**
|
|
8
6
|
* Middleware that manages request session state on request context.
|
|
9
7
|
*
|
|
@@ -14,7 +12,7 @@ type SetSessionContextTransform = readonly [readonly [typeof Session, Session]]
|
|
|
14
12
|
export function session(
|
|
15
13
|
sessionCookie: Cookie,
|
|
16
14
|
sessionStorage: SessionStorage,
|
|
17
|
-
): Middleware<
|
|
15
|
+
): Middleware<{ key: typeof Session; value: Session; property: 'session' }> {
|
|
18
16
|
if (!sessionCookie.signed) {
|
|
19
17
|
throw new Error('Session cookie must be signed')
|
|
20
18
|
}
|
|
@@ -27,7 +25,7 @@ export function session(
|
|
|
27
25
|
let cookieValue = await sessionCookie.parse(context.headers.get('Cookie'))
|
|
28
26
|
let session = await sessionStorage.read(cookieValue)
|
|
29
27
|
|
|
30
|
-
context.set(Session, session)
|
|
28
|
+
context.set(Session, session, { property: 'session' })
|
|
31
29
|
|
|
32
30
|
let response = await next()
|
|
33
31
|
|