@remix-run/session-middleware 0.1.4 → 0.2.1
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 -7
- package/dist/lib/session.d.ts +5 -3
- package/dist/lib/session.d.ts.map +1 -1
- package/dist/lib/session.js +5 -4
- package/package.json +8 -6
- package/src/lib/session.ts +11 -6
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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, stores it in request context using `Session`, and persists updates automatically.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -19,6 +19,7 @@ npm i remix
|
|
|
19
19
|
```ts
|
|
20
20
|
import { createRouter } from 'remix/fetch-router'
|
|
21
21
|
import { createCookie } from 'remix/cookie'
|
|
22
|
+
import { Session } from 'remix/session'
|
|
22
23
|
import { createCookieSessionStorage } from 'remix/session/cookie-storage'
|
|
23
24
|
import { session } from 'remix/session-middleware'
|
|
24
25
|
|
|
@@ -36,15 +37,16 @@ let router = createRouter({
|
|
|
36
37
|
})
|
|
37
38
|
|
|
38
39
|
router.get('/', (context) => {
|
|
39
|
-
|
|
40
|
-
|
|
40
|
+
let session = context.get(Session)
|
|
41
|
+
session.set('count', Number(session.get('count') ?? 0) + 1)
|
|
42
|
+
return new Response(`Count: ${session.get('count')}`)
|
|
41
43
|
})
|
|
42
44
|
```
|
|
43
45
|
|
|
44
46
|
The middleware:
|
|
45
47
|
|
|
46
48
|
- Reads the session from the cookie on incoming requests
|
|
47
|
-
- Makes it available as `context.
|
|
49
|
+
- Makes it available as `context.get(Session)`
|
|
48
50
|
- Automatically saves session changes and sets the cookie on responses
|
|
49
51
|
|
|
50
52
|
Note: The session cookie must be signed for security. This prevents tampering with the session data on the client.
|
|
@@ -55,8 +57,10 @@ A basic login/logout flow could look like this:
|
|
|
55
57
|
|
|
56
58
|
```ts
|
|
57
59
|
import * as res from 'remix/fetch-router/response-helpers'
|
|
60
|
+
import { Session } from 'remix/session'
|
|
58
61
|
|
|
59
|
-
router.get('/login', ({
|
|
62
|
+
router.get('/login', ({ get }) => {
|
|
63
|
+
let session = get(Session)
|
|
60
64
|
let error = session.get('error')
|
|
61
65
|
return res.html(`
|
|
62
66
|
<html>
|
|
@@ -73,7 +77,9 @@ router.get('/login', ({ session }) => {
|
|
|
73
77
|
`)
|
|
74
78
|
})
|
|
75
79
|
|
|
76
|
-
router.post('/login', ({
|
|
80
|
+
router.post('/login', ({ get }) => {
|
|
81
|
+
let session = get(Session)
|
|
82
|
+
let formData = get(FormData)
|
|
77
83
|
let username = formData.get('username')
|
|
78
84
|
let password = formData.get('password')
|
|
79
85
|
|
|
@@ -89,7 +95,8 @@ router.post('/login', ({ session, formData }) => {
|
|
|
89
95
|
return res.redirect('/dashboard')
|
|
90
96
|
})
|
|
91
97
|
|
|
92
|
-
router.post('/logout', ({
|
|
98
|
+
router.post('/logout', ({ get }) => {
|
|
99
|
+
let session = get(Session)
|
|
93
100
|
session.destroy()
|
|
94
101
|
return res.redirect('/')
|
|
95
102
|
})
|
package/dist/lib/session.d.ts
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import type { Cookie } from '@remix-run/cookie';
|
|
2
2
|
import type { Middleware } from '@remix-run/fetch-router';
|
|
3
|
-
import type
|
|
3
|
+
import { Session, type SessionStorage } from '@remix-run/session';
|
|
4
|
+
type SetSessionContextTransform = readonly [readonly [typeof Session, Session]];
|
|
4
5
|
/**
|
|
5
|
-
* Middleware that manages
|
|
6
|
+
* Middleware that manages request session state on request context.
|
|
6
7
|
*
|
|
7
8
|
* @param sessionCookie The session cookie to use
|
|
8
9
|
* @param sessionStorage The storage backend for session data
|
|
9
10
|
* @returns The session middleware
|
|
10
11
|
*/
|
|
11
|
-
export declare function session(sessionCookie: Cookie, sessionStorage: SessionStorage): Middleware
|
|
12
|
+
export declare function session(sessionCookie: Cookie, sessionStorage: SessionStorage): Middleware<any, any, SetSessionContextTransform>;
|
|
13
|
+
export {};
|
|
12
14
|
//# 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,
|
|
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,KAAK,0BAA0B,GAAG,SAAS,CAAC,SAAS,CAAC,OAAO,OAAO,EAAE,OAAO,CAAC,CAAC,CAAA;AAE/E;;;;;;GAMG;AACH,wBAAgB,OAAO,CACrB,aAAa,EAAE,MAAM,EACrB,cAAc,EAAE,cAAc,GAC7B,UAAU,CAAC,GAAG,EAAE,GAAG,EAAE,0BAA0B,CAAC,CA8BlD"}
|
package/dist/lib/session.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { Session } from '@remix-run/session';
|
|
1
2
|
/**
|
|
2
|
-
* Middleware that manages
|
|
3
|
+
* Middleware that manages request session state on request context.
|
|
3
4
|
*
|
|
4
5
|
* @param sessionCookie The session cookie to use
|
|
5
6
|
* @param sessionStorage The storage backend for session data
|
|
@@ -10,14 +11,14 @@ export function session(sessionCookie, sessionStorage) {
|
|
|
10
11
|
throw new Error('Session cookie must be signed');
|
|
11
12
|
}
|
|
12
13
|
return async (context, next) => {
|
|
13
|
-
if (context.
|
|
14
|
+
if (context.has(Session)) {
|
|
14
15
|
throw new Error('Existing session found, refusing to overwrite');
|
|
15
16
|
}
|
|
16
17
|
let cookieValue = await sessionCookie.parse(context.headers.get('Cookie'));
|
|
17
18
|
let session = await sessionStorage.read(cookieValue);
|
|
18
|
-
context.
|
|
19
|
+
context.set(Session, session);
|
|
19
20
|
let response = await next();
|
|
20
|
-
if (session !== context.
|
|
21
|
+
if (session !== context.get(Session)) {
|
|
21
22
|
throw new Error('Cannot save session that was initialized by another middleware/handler');
|
|
22
23
|
}
|
|
23
24
|
let setCookieValue = await sessionStorage.save(session);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix-run/session-middleware",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Middleware for managing sessions with cookie-based storage",
|
|
5
5
|
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,15 +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.1.0",
|
|
31
32
|
"@remix-run/cookie": "0.5.1",
|
|
33
|
+
"@remix-run/headers": "0.19.0",
|
|
34
|
+
"@remix-run/fetch-router": "0.18.1",
|
|
32
35
|
"@remix-run/session": "0.4.1",
|
|
33
|
-
"@remix-run/
|
|
34
|
-
"@remix-run/headers": "0.19.0"
|
|
36
|
+
"@remix-run/test": "0.1.0"
|
|
35
37
|
},
|
|
36
38
|
"dependencies": {
|
|
37
39
|
"@remix-run/cookie": "^0.5.1",
|
|
38
|
-
"@remix-run/
|
|
39
|
-
"@remix-run/
|
|
40
|
+
"@remix-run/session": "^0.4.1",
|
|
41
|
+
"@remix-run/fetch-router": "^0.18.1"
|
|
40
42
|
},
|
|
41
43
|
"keywords": [
|
|
42
44
|
"fetch",
|
|
@@ -49,7 +51,7 @@
|
|
|
49
51
|
"scripts": {
|
|
50
52
|
"build": "tsgo -p tsconfig.build.json",
|
|
51
53
|
"clean": "git clean -fdX",
|
|
52
|
-
"test": "
|
|
54
|
+
"test": "remix-test",
|
|
53
55
|
"typecheck": "tsgo --noEmit"
|
|
54
56
|
}
|
|
55
57
|
}
|
package/src/lib/session.ts
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
1
|
import type { Cookie } from '@remix-run/cookie'
|
|
2
2
|
import type { Middleware } from '@remix-run/fetch-router'
|
|
3
|
-
import type
|
|
3
|
+
import { Session, type SessionStorage } from '@remix-run/session'
|
|
4
|
+
|
|
5
|
+
type SetSessionContextTransform = readonly [readonly [typeof Session, Session]]
|
|
4
6
|
|
|
5
7
|
/**
|
|
6
|
-
* Middleware that manages
|
|
8
|
+
* Middleware that manages request session state on request context.
|
|
7
9
|
*
|
|
8
10
|
* @param sessionCookie The session cookie to use
|
|
9
11
|
* @param sessionStorage The storage backend for session data
|
|
10
12
|
* @returns The session middleware
|
|
11
13
|
*/
|
|
12
|
-
export function session(
|
|
14
|
+
export function session(
|
|
15
|
+
sessionCookie: Cookie,
|
|
16
|
+
sessionStorage: SessionStorage,
|
|
17
|
+
): Middleware<any, any, SetSessionContextTransform> {
|
|
13
18
|
if (!sessionCookie.signed) {
|
|
14
19
|
throw new Error('Session cookie must be signed')
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
return async (context, next) => {
|
|
18
|
-
if (context.
|
|
23
|
+
if (context.has(Session)) {
|
|
19
24
|
throw new Error('Existing session found, refusing to overwrite')
|
|
20
25
|
}
|
|
21
26
|
|
|
22
27
|
let cookieValue = await sessionCookie.parse(context.headers.get('Cookie'))
|
|
23
28
|
let session = await sessionStorage.read(cookieValue)
|
|
24
29
|
|
|
25
|
-
context.
|
|
30
|
+
context.set(Session, session)
|
|
26
31
|
|
|
27
32
|
let response = await next()
|
|
28
33
|
|
|
29
|
-
if (session !== context.
|
|
34
|
+
if (session !== context.get(Session)) {
|
|
30
35
|
throw new Error('Cannot save session that was initialized by another middleware/handler')
|
|
31
36
|
}
|
|
32
37
|
|