@remix-run/session-middleware 0.0.0 → 0.1.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 +16 -15
- package/dist/lib/session.d.ts +6 -5
- package/dist/lib/session.d.ts.map +1 -1
- package/dist/lib/session.js +10 -9
- package/package.json +12 -11
- package/src/index.ts +0 -1
- package/src/lib/session.ts +11 -12
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ npm install @remix-run/session-middleware
|
|
|
13
13
|
```ts
|
|
14
14
|
import { createRouter } from '@remix-run/fetch-router'
|
|
15
15
|
import { createCookie } from '@remix-run/cookie'
|
|
16
|
-
import {
|
|
16
|
+
import { createCookieSessionStorage } from '@remix-run/session/cookie-storage'
|
|
17
17
|
import { session } from '@remix-run/session-middleware'
|
|
18
18
|
|
|
19
19
|
let sessionCookie = createCookie('__session', {
|
|
@@ -23,7 +23,7 @@ let sessionCookie = createCookie('__session', {
|
|
|
23
23
|
sameSite: 'lax',
|
|
24
24
|
})
|
|
25
25
|
|
|
26
|
-
let sessionStorage =
|
|
26
|
+
let sessionStorage = createCookieSessionStorage()
|
|
27
27
|
|
|
28
28
|
let router = createRouter({
|
|
29
29
|
middleware: [session(sessionCookie, sessionStorage)],
|
|
@@ -49,21 +49,22 @@ A basic login/logout flow could look like this:
|
|
|
49
49
|
|
|
50
50
|
```ts
|
|
51
51
|
import * as res from '@remix-run/fetch-router/response-helpers'
|
|
52
|
-
import { html } from '@remix-run/html-template'
|
|
53
52
|
|
|
54
53
|
router.get('/login', ({ session }) => {
|
|
55
54
|
let error = session.get('error')
|
|
56
|
-
return res.html(
|
|
57
|
-
html
|
|
58
|
-
<
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
<
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
55
|
+
return res.html(`
|
|
56
|
+
<html>
|
|
57
|
+
<body>
|
|
58
|
+
<h1>Login</h1>
|
|
59
|
+
${typeof error === 'string' ? <div class="error">${error}</div> : null}
|
|
60
|
+
<form method="POST" action="/login">
|
|
61
|
+
<input type="text" name="username" placeholder="Username" />
|
|
62
|
+
<input type="password" name="password" placeholder="Password" />
|
|
63
|
+
<button type="submit">Login</button>
|
|
64
|
+
</form>
|
|
65
|
+
</body>
|
|
66
|
+
</html>
|
|
67
|
+
`)
|
|
67
68
|
})
|
|
68
69
|
|
|
69
70
|
router.post('/login', ({ session, formData }) => {
|
|
@@ -76,8 +77,8 @@ router.post('/login', ({ session, formData }) => {
|
|
|
76
77
|
return res.redirect('/login')
|
|
77
78
|
}
|
|
78
79
|
|
|
79
|
-
session.set('userId', user.id)
|
|
80
80
|
session.regenerateId()
|
|
81
|
+
session.set('userId', user.id)
|
|
81
82
|
|
|
82
83
|
return res.redirect('/dashboard')
|
|
83
84
|
})
|
package/dist/lib/session.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { Cookie } from '@remix-run/cookie';
|
|
2
|
-
import type { SessionStorage } from '@remix-run/session';
|
|
3
2
|
import type { Middleware } from '@remix-run/fetch-router';
|
|
3
|
+
import type { SessionStorage } from '@remix-run/session';
|
|
4
4
|
/**
|
|
5
5
|
* Middleware that manages `context.session` based on the session cookie.
|
|
6
|
-
*
|
|
7
|
-
* @param
|
|
8
|
-
* @
|
|
6
|
+
*
|
|
7
|
+
* @param sessionCookie The session cookie to use
|
|
8
|
+
* @param sessionStorage The storage backend for session data
|
|
9
|
+
* @return The session middleware
|
|
9
10
|
*/
|
|
10
|
-
export declare function session(
|
|
11
|
+
export declare function session(sessionCookie: Cookie, sessionStorage: SessionStorage): Middleware;
|
|
11
12
|
//# 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,
|
|
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,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD;;;;;;GAMG;AACH,wBAAgB,OAAO,CAAC,aAAa,EAAE,MAAM,EAAE,cAAc,EAAE,cAAc,GAAG,UAAU,CA4BzF"}
|
package/dist/lib/session.js
CHANGED
|
@@ -1,27 +1,28 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Middleware that manages `context.session` based on the session cookie.
|
|
3
|
-
*
|
|
4
|
-
* @param
|
|
5
|
-
* @
|
|
3
|
+
*
|
|
4
|
+
* @param sessionCookie The session cookie to use
|
|
5
|
+
* @param sessionStorage The storage backend for session data
|
|
6
|
+
* @return The session middleware
|
|
6
7
|
*/
|
|
7
|
-
export function session(
|
|
8
|
-
if (!
|
|
8
|
+
export function session(sessionCookie, sessionStorage) {
|
|
9
|
+
if (!sessionCookie.signed) {
|
|
9
10
|
throw new Error('Session cookie must be signed');
|
|
10
11
|
}
|
|
11
12
|
return async (context, next) => {
|
|
12
13
|
if (context.sessionStarted) {
|
|
13
14
|
throw new Error('Existing session found, refusing to overwrite');
|
|
14
15
|
}
|
|
15
|
-
let cookieValue = await
|
|
16
|
-
let session = await
|
|
16
|
+
let cookieValue = await sessionCookie.parse(context.headers.get('Cookie'));
|
|
17
|
+
let session = await sessionStorage.read(cookieValue);
|
|
17
18
|
context.session = session;
|
|
18
19
|
let response = await next();
|
|
19
20
|
if (session !== context.session) {
|
|
20
21
|
throw new Error('Cannot save session that was initialized by another middleware/handler');
|
|
21
22
|
}
|
|
22
|
-
let setCookieValue = await
|
|
23
|
+
let setCookieValue = await sessionStorage.save(session);
|
|
23
24
|
if (setCookieValue != null) {
|
|
24
|
-
response.headers.
|
|
25
|
+
response.headers.append('Set-Cookie', await sessionCookie.serialize(setCookieValue));
|
|
25
26
|
}
|
|
26
27
|
return response;
|
|
27
28
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix-run/session-middleware",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Middleware for managing sessions with cookie-based storage",
|
|
5
5
|
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -27,16 +27,17 @@
|
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^24.6.0",
|
|
30
|
+
"@typescript/native-preview": "7.0.0-dev.20251125.1",
|
|
30
31
|
"typescript": "^5.9.3",
|
|
31
|
-
"@remix-run/
|
|
32
|
-
"@remix-run/
|
|
33
|
-
"@remix-run/
|
|
34
|
-
"@remix-run/session": "0.
|
|
32
|
+
"@remix-run/cookie": "0.5.0",
|
|
33
|
+
"@remix-run/fetch-router": "0.13.0",
|
|
34
|
+
"@remix-run/headers": "0.18.0",
|
|
35
|
+
"@remix-run/session": "0.4.1"
|
|
35
36
|
},
|
|
36
37
|
"peerDependencies": {
|
|
37
|
-
"@remix-run/cookie": "^0.
|
|
38
|
-
"@remix-run/fetch-router": "^0.
|
|
39
|
-
"@remix-run/session": "^0.
|
|
38
|
+
"@remix-run/cookie": "^0.5.0",
|
|
39
|
+
"@remix-run/fetch-router": "^0.13.0",
|
|
40
|
+
"@remix-run/session": "^0.4.1"
|
|
40
41
|
},
|
|
41
42
|
"keywords": [
|
|
42
43
|
"fetch",
|
|
@@ -47,9 +48,9 @@
|
|
|
47
48
|
"session-management"
|
|
48
49
|
],
|
|
49
50
|
"scripts": {
|
|
50
|
-
"build": "
|
|
51
|
+
"build": "tsgo -p tsconfig.build.json",
|
|
51
52
|
"clean": "git clean -fdX",
|
|
52
|
-
"test": "node --disable-warning=ExperimentalWarning --test
|
|
53
|
-
"typecheck": "
|
|
53
|
+
"test": "node --disable-warning=ExperimentalWarning --test",
|
|
54
|
+
"typecheck": "tsgo --noEmit"
|
|
54
55
|
}
|
|
55
56
|
}
|
package/src/index.ts
CHANGED
package/src/lib/session.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import type { Cookie } from '@remix-run/cookie'
|
|
2
|
-
import type { SessionStorage } from '@remix-run/session'
|
|
3
|
-
|
|
4
2
|
import type { Middleware } from '@remix-run/fetch-router'
|
|
3
|
+
import type { SessionStorage } from '@remix-run/session'
|
|
5
4
|
|
|
6
5
|
/**
|
|
7
6
|
* Middleware that manages `context.session` based on the session cookie.
|
|
8
|
-
*
|
|
9
|
-
* @param
|
|
10
|
-
* @
|
|
7
|
+
*
|
|
8
|
+
* @param sessionCookie The session cookie to use
|
|
9
|
+
* @param sessionStorage The storage backend for session data
|
|
10
|
+
* @return The session middleware
|
|
11
11
|
*/
|
|
12
|
-
export function session(
|
|
13
|
-
if (!
|
|
12
|
+
export function session(sessionCookie: Cookie, sessionStorage: SessionStorage): Middleware {
|
|
13
|
+
if (!sessionCookie.signed) {
|
|
14
14
|
throw new Error('Session cookie must be signed')
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -19,8 +19,8 @@ export function session(cookie: Cookie, storage: SessionStorage): Middleware {
|
|
|
19
19
|
throw new Error('Existing session found, refusing to overwrite')
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
let cookieValue = await
|
|
23
|
-
let session = await
|
|
22
|
+
let cookieValue = await sessionCookie.parse(context.headers.get('Cookie'))
|
|
23
|
+
let session = await sessionStorage.read(cookieValue)
|
|
24
24
|
|
|
25
25
|
context.session = session
|
|
26
26
|
|
|
@@ -30,12 +30,11 @@ export function session(cookie: Cookie, storage: SessionStorage): Middleware {
|
|
|
30
30
|
throw new Error('Cannot save session that was initialized by another middleware/handler')
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
let setCookieValue = await
|
|
33
|
+
let setCookieValue = await sessionStorage.save(session)
|
|
34
34
|
if (setCookieValue != null) {
|
|
35
|
-
response.headers.
|
|
35
|
+
response.headers.append('Set-Cookie', await sessionCookie.serialize(setCookieValue))
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
return response
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
-
|