@remix-run/session-middleware 0.0.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/LICENSE +22 -0
- package/README.md +99 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/session.d.ts +11 -0
- package/dist/lib/session.d.ts.map +1 -0
- package/dist/lib/session.js +28 -0
- package/package.json +55 -0
- package/src/index.ts +2 -0
- package/src/lib/session.ts +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Shopify Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# session-middleware
|
|
2
|
+
|
|
3
|
+
Middleware for managing sessions with [`@remix-run/fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) via securely signed cookies.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @remix-run/session-middleware
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { createRouter } from '@remix-run/fetch-router'
|
|
15
|
+
import { createCookie } from '@remix-run/cookie'
|
|
16
|
+
import { createCookieStorage } from '@remix-run/session/cookie-storage'
|
|
17
|
+
import { session } from '@remix-run/session-middleware'
|
|
18
|
+
|
|
19
|
+
let sessionCookie = createCookie('__session', {
|
|
20
|
+
secrets: ['s3cr3t'], // session cookies must be signed!
|
|
21
|
+
httpOnly: true,
|
|
22
|
+
secure: true,
|
|
23
|
+
sameSite: 'lax',
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
let sessionStorage = createCookieStorage()
|
|
27
|
+
|
|
28
|
+
let router = createRouter({
|
|
29
|
+
middleware: [session(sessionCookie, sessionStorage)],
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
router.get('/', (context) => {
|
|
33
|
+
context.session.set('count', Number(context.session.get('count') ?? 0) + 1)
|
|
34
|
+
return new Response(`Count: ${context.session.get('count')}`)
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The middleware:
|
|
39
|
+
|
|
40
|
+
- Reads the session from the cookie on incoming requests
|
|
41
|
+
- Makes it available as `context.session`
|
|
42
|
+
- Automatically saves session changes and sets the cookie on responses
|
|
43
|
+
|
|
44
|
+
Note: The session cookie must be signed for security. This prevents tampering with the session data on the client.
|
|
45
|
+
|
|
46
|
+
### Login/Logout Flow
|
|
47
|
+
|
|
48
|
+
A basic login/logout flow could look like this:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
import * as res from '@remix-run/fetch-router/response-helpers'
|
|
52
|
+
import { html } from '@remix-run/html-template'
|
|
53
|
+
|
|
54
|
+
router.get('/login', ({ session }) => {
|
|
55
|
+
let error = session.get('error')
|
|
56
|
+
return res.html(
|
|
57
|
+
html` <div>
|
|
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
|
+
</div>`,
|
|
66
|
+
)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
router.post('/login', ({ session, formData }) => {
|
|
70
|
+
let username = formData.get('username')
|
|
71
|
+
let password = formData.get('password')
|
|
72
|
+
|
|
73
|
+
let user = authenticateUser(username, password)
|
|
74
|
+
if (!user) {
|
|
75
|
+
session.flash('error', 'Invalid username or password')
|
|
76
|
+
return res.redirect('/login')
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
session.set('userId', user.id)
|
|
80
|
+
session.regenerateId()
|
|
81
|
+
|
|
82
|
+
return res.redirect('/dashboard')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
router.post('/logout', ({ session }) => {
|
|
86
|
+
session.destroy()
|
|
87
|
+
return res.redirect('/')
|
|
88
|
+
})
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Related Packages
|
|
92
|
+
|
|
93
|
+
- [`fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) - Router for the web Fetch API
|
|
94
|
+
- [`session`](https://github.com/remix-run/remix/tree/main/packages/session) - Session management and storage
|
|
95
|
+
- [`cookie`](https://github.com/remix-run/remix/tree/main/packages/cookie) - Cookie parsing and serialization
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
See [LICENSE](https://github.com/remix-run/remix/blob/main/LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { session } from "./lib/session.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Cookie } from '@remix-run/cookie';
|
|
2
|
+
import type { SessionStorage } from '@remix-run/session';
|
|
3
|
+
import type { Middleware } from '@remix-run/fetch-router';
|
|
4
|
+
/**
|
|
5
|
+
* Middleware that manages `context.session` based on the session cookie.
|
|
6
|
+
* @param cookie The session cookie to use
|
|
7
|
+
* @param storage The storage backend for session data
|
|
8
|
+
* @returns The session middleware
|
|
9
|
+
*/
|
|
10
|
+
export declare function session(cookie: Cookie, storage: SessionStorage): Middleware;
|
|
11
|
+
//# sourceMappingURL=session.d.ts.map
|
|
@@ -0,0 +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,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAExD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAEzD;;;;;GAKG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,cAAc,GAAG,UAAU,CA4B3E"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Middleware that manages `context.session` based on the session cookie.
|
|
3
|
+
* @param cookie The session cookie to use
|
|
4
|
+
* @param storage The storage backend for session data
|
|
5
|
+
* @returns The session middleware
|
|
6
|
+
*/
|
|
7
|
+
export function session(cookie, storage) {
|
|
8
|
+
if (!cookie.signed) {
|
|
9
|
+
throw new Error('Session cookie must be signed');
|
|
10
|
+
}
|
|
11
|
+
return async (context, next) => {
|
|
12
|
+
if (context.sessionStarted) {
|
|
13
|
+
throw new Error('Existing session found, refusing to overwrite');
|
|
14
|
+
}
|
|
15
|
+
let cookieValue = await cookie.parse(context.headers.get('Cookie'));
|
|
16
|
+
let session = await storage.read(cookieValue);
|
|
17
|
+
context.session = session;
|
|
18
|
+
let response = await next();
|
|
19
|
+
if (session !== context.session) {
|
|
20
|
+
throw new Error('Cannot save session that was initialized by another middleware/handler');
|
|
21
|
+
}
|
|
22
|
+
let setCookieValue = await storage.save(session);
|
|
23
|
+
if (setCookieValue != null) {
|
|
24
|
+
response.headers.set('Set-Cookie', await cookie.serialize(setCookieValue));
|
|
25
|
+
}
|
|
26
|
+
return response;
|
|
27
|
+
};
|
|
28
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@remix-run/session-middleware",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Middleware for managing sessions with cookie-based storage",
|
|
5
|
+
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/remix-run/remix.git",
|
|
10
|
+
"directory": "packages/session-middleware"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/remix-run/remix/tree/main/packages/session-middleware#readme",
|
|
13
|
+
"files": [
|
|
14
|
+
"LICENSE",
|
|
15
|
+
"README.md",
|
|
16
|
+
"dist",
|
|
17
|
+
"src",
|
|
18
|
+
"!src/**/*.test.ts"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"default": "./dist/index.js"
|
|
25
|
+
},
|
|
26
|
+
"./package.json": "./package.json"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@types/node": "^24.6.0",
|
|
30
|
+
"typescript": "^5.9.3",
|
|
31
|
+
"@remix-run/fetch-router": "0.9.0",
|
|
32
|
+
"@remix-run/headers": "0.17.0",
|
|
33
|
+
"@remix-run/cookie": "0.4.1",
|
|
34
|
+
"@remix-run/session": "0.2.1"
|
|
35
|
+
},
|
|
36
|
+
"peerDependencies": {
|
|
37
|
+
"@remix-run/cookie": "^0.4.1",
|
|
38
|
+
"@remix-run/fetch-router": "^0.9.0",
|
|
39
|
+
"@remix-run/session": "^0.2.1"
|
|
40
|
+
},
|
|
41
|
+
"keywords": [
|
|
42
|
+
"fetch",
|
|
43
|
+
"router",
|
|
44
|
+
"middleware",
|
|
45
|
+
"session",
|
|
46
|
+
"cookie",
|
|
47
|
+
"session-management"
|
|
48
|
+
],
|
|
49
|
+
"scripts": {
|
|
50
|
+
"build": "tsc -p tsconfig.build.json",
|
|
51
|
+
"clean": "git clean -fdX",
|
|
52
|
+
"test": "node --disable-warning=ExperimentalWarning --test './src/**/*.test.ts'",
|
|
53
|
+
"typecheck": "tsc --noEmit"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Cookie } from '@remix-run/cookie'
|
|
2
|
+
import type { SessionStorage } from '@remix-run/session'
|
|
3
|
+
|
|
4
|
+
import type { Middleware } from '@remix-run/fetch-router'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Middleware that manages `context.session` based on the session cookie.
|
|
8
|
+
* @param cookie The session cookie to use
|
|
9
|
+
* @param storage The storage backend for session data
|
|
10
|
+
* @returns The session middleware
|
|
11
|
+
*/
|
|
12
|
+
export function session(cookie: Cookie, storage: SessionStorage): Middleware {
|
|
13
|
+
if (!cookie.signed) {
|
|
14
|
+
throw new Error('Session cookie must be signed')
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return async (context, next) => {
|
|
18
|
+
if (context.sessionStarted) {
|
|
19
|
+
throw new Error('Existing session found, refusing to overwrite')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
let cookieValue = await cookie.parse(context.headers.get('Cookie'))
|
|
23
|
+
let session = await storage.read(cookieValue)
|
|
24
|
+
|
|
25
|
+
context.session = session
|
|
26
|
+
|
|
27
|
+
let response = await next()
|
|
28
|
+
|
|
29
|
+
if (session !== context.session) {
|
|
30
|
+
throw new Error('Cannot save session that was initialized by another middleware/handler')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let setCookieValue = await storage.save(session)
|
|
34
|
+
if (setCookieValue != null) {
|
|
35
|
+
response.headers.set('Set-Cookie', await cookie.serialize(setCookieValue))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return response
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|