@lenne.tech/nest-server 11.43.7 → 11.43.9
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/.claude/rules/better-auth.md +12 -2
- package/.claude/rules/testing.md +4 -4
- package/FRAMEWORK-API.md +1 -1
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js +21 -0
- package/dist/core/modules/better-auth/core-better-auth-api.middleware.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js +24 -2
- package/dist/core/modules/better-auth/core-better-auth-session-token.helper.js.map +1 -1
- package/dist/core/modules/better-auth/core-better-auth.service.d.ts +1 -0
- package/dist/core/modules/better-auth/core-better-auth.service.js +15 -0
- package/dist/core/modules/better-auth/core-better-auth.service.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/docs/REQUEST-LIFECYCLE.md +5 -3
- package/migration-guides/11.43.7-to-11.43.8.md +91 -0
- package/migration-guides/11.43.8-to-11.43.9.md +43 -0
- package/package.json +7 -7
- package/src/core/modules/better-auth/INTEGRATION-CHECKLIST.md +2 -2
- package/src/core/modules/better-auth/core-better-auth-api.middleware.ts +41 -0
- package/src/core/modules/better-auth/core-better-auth-session-token.helper.ts +41 -4
- package/src/core/modules/better-auth/core-better-auth.service.ts +29 -0
|
@@ -683,9 +683,11 @@ If authentication succeeds, `req.user` is set with the authenticated user (inclu
|
|
|
683
683
|
> Before, two-factor verify, passkey verify, OTP / magic-link sign-in and `get-session` answered with the
|
|
684
684
|
> opaque session token in the body, also in cookie mode. The session token is recognised by the session
|
|
685
685
|
> cookie the response sets (under the name the instance gives it), by `session.token`, or — for a top-level
|
|
686
|
-
> token neither identifies, as a signed-in user's second factor returns — by a session-store lookup.
|
|
687
|
-
>
|
|
688
|
-
>
|
|
686
|
+
> token neither identifies, as a signed-in user's second factor returns — by a session-store lookup. Since
|
|
687
|
+
> 11.43.8 a session record loses its `token` also inside an array (`list-sessions` names sessions by `id`;
|
|
688
|
+
> `POST /revoke-session` takes `{ sessionId }`, resolved by the API middleware among the caller's own
|
|
689
|
+
> sessions). A JWT (`/token`) and other plugins' tokens are left alone. See `.claude/rules/better-auth.md` §7
|
|
690
|
+
> "Forwarded responses".
|
|
689
691
|
|
|
690
692
|
#### 2b. SecurityHeadersMiddleware
|
|
691
693
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Migration Guide: 11.43.7 → 11.43.8
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None for the server API. `GET /iam/list-sessions` no longer carries a `token` field per session (§1) |
|
|
8
|
+
| **New Features** | `POST /iam/revoke-session` accepts `{ sessionId }` (§1) |
|
|
9
|
+
| **Bugfixes** | Security: `GET /iam/list-sessions` answered every session of the user with its session token (§1) |
|
|
10
|
+
| **Migration Effort** | None on the server. A "signed-in devices" screen that revokes a session by its `token` sends the `id` instead (§1). Pin `better-auth`, `@better-auth/core` and `@better-auth/passkey` to 1.7.16 (§2) |
|
|
11
|
+
|
|
12
|
+
11.43.8 is a security patch on the 11.x line, published under the dist-tag `latest-11` (backport of 12.6.2 / 12.7.0).
|
|
13
|
+
|
|
14
|
+
## Quick Migration
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pnpm add @lenne.tech/nest-server@11.43.8
|
|
18
|
+
pnpm run update # nest-server-starter based projects
|
|
19
|
+
pnpm run build
|
|
20
|
+
pnpm test
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Vendor-mode projects update the core to the tag `11.43.8`: `/lt-dev:backend:update-nest-server-core --target 11.43.8`.
|
|
24
|
+
Without `--target` the updater syncs to the latest tag, which is on the 12.x line.
|
|
25
|
+
|
|
26
|
+
## 1. list-sessions names sessions by id; revoke-session takes the id
|
|
27
|
+
|
|
28
|
+
**Why:** `GET /iam/list-sessions` answered every session of the user with its `token` — the bearer credential of every
|
|
29
|
+
device the user is signed in on, readable by any script on the page. 11.43.7 took the session token out of the other
|
|
30
|
+
answers the framework forwards to Better-Auth; this was the last one.
|
|
31
|
+
|
|
32
|
+
**What changed:**
|
|
33
|
+
|
|
34
|
+
| | Until 11.43.7 | From 11.43.8 |
|
|
35
|
+
|---|---|---|
|
|
36
|
+
| `GET /iam/list-sessions` | `[{ id, token, userId, expiresAt, … }]` | `[{ id, userId, expiresAt, … }]` — no `token`, in every mode |
|
|
37
|
+
| `POST /iam/revoke-session` | `{ token }` | `{ sessionId }` (new) or `{ token }` (unchanged) |
|
|
38
|
+
|
|
39
|
+
`{ sessionId }` is resolved among the sessions of the signed-in user. An unknown id, or the id of another user's
|
|
40
|
+
session, revokes nothing — the answer is the same as for a foreign token, so it discloses nothing. Revoking by `token`
|
|
41
|
+
stays for clients that hold one.
|
|
42
|
+
|
|
43
|
+
**Your frontend:** a "signed-in devices" screen that revokes a session by the `token` from `list-sessions` sends the
|
|
44
|
+
`id` instead:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
await fetch(`${api}/iam/revoke-session`, {
|
|
48
|
+
body: JSON.stringify({ sessionId: session.id }),
|
|
49
|
+
credentials: 'include',
|
|
50
|
+
headers: { 'Content-Type': 'application/json' },
|
|
51
|
+
method: 'POST',
|
|
52
|
+
});
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Better-Auth's client (`authClient.revokeSession({ token })`) types the body as `{ token }`; send the request yourself
|
|
56
|
+
as above.
|
|
57
|
+
|
|
58
|
+
## 2. better-auth 1.7.16 or newer
|
|
59
|
+
|
|
60
|
+
The peer range of `better-auth`, `@better-auth/core` and `@better-auth/passkey` is now `>=1.7.16 <1.8.0` (was
|
|
61
|
+
`>=1.7.7 <1.8.0`), the same as on the 12.6 line, and this release is tested against 1.7.16. `@lenne.tech/nuxt-extensions`
|
|
62
|
+
1.26.0 requires `>=1.7.16` too, so a fullstack project's workspace check (same range in API and app) passes again.
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pnpm add better-auth@1.7.16 @better-auth/passkey@1.7.16 @better-auth/core@1.7.16
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Keep all three on the same exact version, in the API and in the app.
|
|
69
|
+
|
|
70
|
+
## Compatibility Notes
|
|
71
|
+
|
|
72
|
+
| Pattern | Status |
|
|
73
|
+
|---|---|
|
|
74
|
+
| A client reading `token` from `list-sessions` | Gets none; use `id` and `revoke-session` with `{ sessionId }` |
|
|
75
|
+
| `revoke-session` with `{ token }` | Unchanged |
|
|
76
|
+
| `revoke-sessions`, `revoke-other-sessions`, sign-out | Unchanged |
|
|
77
|
+
| better-auth pinned to 1.7.7 – 1.7.15 | Below the new peer range; raise all three packages to 1.7.16 (§2) |
|
|
78
|
+
| A project subclass of `CoreBetterAuthService` / `CoreBetterAuthApiMiddleware` | `sessionTokenOfUser()` (public) and `resolveRevokeSessionId()` (protected) are new |
|
|
79
|
+
|
|
80
|
+
## Troubleshooting
|
|
81
|
+
|
|
82
|
+
| Symptom | Cause | Fix |
|
|
83
|
+
|---|---|---|
|
|
84
|
+
| Revoking a device does nothing after the update | The screen sends the `token` from `list-sessions`, which is gone | Send `{ sessionId: session.id }` |
|
|
85
|
+
|
|
86
|
+
## References
|
|
87
|
+
|
|
88
|
+
- `src/core/modules/better-auth/core-better-auth-session-token.helper.ts` — session records
|
|
89
|
+
- `src/core/modules/better-auth/core-better-auth-api.middleware.ts` — `resolveRevokeSessionId()`
|
|
90
|
+
- `src/core/modules/better-auth/core-better-auth.service.ts` — `sessionTokenOfUser()`
|
|
91
|
+
- `tests/session-token-exposure.e2e-spec.ts`
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Migration Guide: 11.43.8 → 11.43.9
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
| Category | Details |
|
|
6
|
+
|---|---|
|
|
7
|
+
| **Breaking Changes** | None |
|
|
8
|
+
| **New Features** | None |
|
|
9
|
+
| **Bugfixes** | `POST /iam/revoke-session` with `{ sessionId }` ended nothing for a user the legacy migration linked (§1) |
|
|
10
|
+
| **Migration Effort** | None |
|
|
11
|
+
|
|
12
|
+
11.43.9 is a patch on the 11.x line, published under the dist-tag `latest-11`. The same fix ships in 12.7.2 (`latest`)
|
|
13
|
+
and 12.6.3 (`latest-12.6`).
|
|
14
|
+
|
|
15
|
+
## Quick Migration
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @lenne.tech/nest-server@11.43.9
|
|
19
|
+
pnpm run update # nest-server-starter based projects
|
|
20
|
+
pnpm run build
|
|
21
|
+
pnpm test
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Vendor mode**: `/lt-dev:backend:update-nest-server-core --target 11.43.9` (without `--target` the updater syncs to the
|
|
25
|
+
latest tag, which is on a newer line).
|
|
26
|
+
|
|
27
|
+
## 1. `revoke-session` by id for users the legacy migration linked
|
|
28
|
+
|
|
29
|
+
**Why:** since 11.43.8, `POST /iam/revoke-session` accepts `{ sessionId }` and looks the session up among the caller's own
|
|
30
|
+
sessions. It looked the caller up under `user.id`. The session lookup prefers a **stored** `id` field over `_id`. When
|
|
31
|
+
the legacy migration links a user (on its first IAM sign-in, for example a user created through `POST /users` or the
|
|
32
|
+
legacy sign-up), it writes an `id` of its own next to `_id`. Every session references `_id`. So for such a user no
|
|
33
|
+
session was found, nothing was revoked, and the request still answered `{ status: true }`. The error went toward the
|
|
34
|
+
safe side: never another user's session.
|
|
35
|
+
|
|
36
|
+
**What changed:** the caller is looked up under the `userId` of its own session. The ownership check is unchanged.
|
|
37
|
+
|
|
38
|
+
**Your project:** nothing to do. A project that copied `resolveRevokeSessionId()` into its own core takes the session's
|
|
39
|
+
`userId` first (`req.betterAuthSession.session.userId`).
|
|
40
|
+
|
|
41
|
+
## References
|
|
42
|
+
|
|
43
|
+
- `src/core/modules/better-auth/core-better-auth-api.middleware.ts`: `resolveRevokeSessionId()`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nest-server",
|
|
3
|
-
"version": "11.43.
|
|
3
|
+
"version": "11.43.9",
|
|
4
4
|
"description": "Modern, fast, powerful Node.js web framework in TypeScript based on Nest with a GraphQL API and a connection to MongoDB (or other databases).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node",
|
|
@@ -145,10 +145,10 @@
|
|
|
145
145
|
"@aws-sdk/client-s3": ">=3.1045.0 <4",
|
|
146
146
|
"@aws-sdk/lib-storage": ">=3.1045.0 <4",
|
|
147
147
|
"@aws-sdk/s3-request-presigner": ">=3.1045.0 <4",
|
|
148
|
-
"@better-auth/core": ">=1.7.
|
|
149
|
-
"@better-auth/passkey": ">=1.7.
|
|
148
|
+
"@better-auth/core": ">=1.7.16 <1.8.0",
|
|
149
|
+
"@better-auth/passkey": ">=1.7.16 <1.8.0",
|
|
150
150
|
"@tus/s3-store": ">=2.0.5 <3",
|
|
151
|
-
"better-auth": ">=1.7.
|
|
151
|
+
"better-auth": ">=1.7.16 <1.8.0",
|
|
152
152
|
"bullmq": ">=5.16.0 <7",
|
|
153
153
|
"ioredis": ">=5.0.0 <7"
|
|
154
154
|
},
|
|
@@ -185,8 +185,8 @@
|
|
|
185
185
|
"@aws-sdk/client-s3": "3.1152.0",
|
|
186
186
|
"@aws-sdk/lib-storage": "3.1152.0",
|
|
187
187
|
"@aws-sdk/s3-request-presigner": "3.1152.0",
|
|
188
|
-
"@better-auth/core": "1.7.
|
|
189
|
-
"@better-auth/passkey": "1.7.
|
|
188
|
+
"@better-auth/core": "1.7.16",
|
|
189
|
+
"@better-auth/passkey": "1.7.16",
|
|
190
190
|
"@compodoc/compodoc": "2.0.0",
|
|
191
191
|
"@nestjs/cli": "11.0.24",
|
|
192
192
|
"@nestjs/schematics": "11.1.0",
|
|
@@ -203,7 +203,7 @@
|
|
|
203
203
|
"@types/node": "26.6.5",
|
|
204
204
|
"@types/passport": "1.0.17",
|
|
205
205
|
"@vitest/coverage-v8": "4.1.12",
|
|
206
|
-
"better-auth": "1.7.
|
|
206
|
+
"better-auth": "1.7.16",
|
|
207
207
|
"bullmq": "6.3.16",
|
|
208
208
|
"cross-env": "10.1.0",
|
|
209
209
|
"ioredis": "6.0.0",
|
|
@@ -18,14 +18,14 @@ From `@lenne.tech/nest-server` 11.37.0, better-auth is **not** a dependency of t
|
|
|
18
18
|
non-optional **peer dependency**, so your project declares and owns the version:
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
|
-
pnpm add better-auth@1.7.
|
|
21
|
+
pnpm add better-auth@1.7.16 @better-auth/passkey@1.7.16 @better-auth/core@1.7.16
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
**Pin all three to the same exact version** — no `^`, no `~`. They are one release train, and a
|
|
25
25
|
mixed set fails in ways the type checker does not catch.
|
|
26
26
|
|
|
27
27
|
> **Do not rely on `pnpm install` to remind you.** On pnpm's default `autoInstallPeers: true` the
|
|
28
|
-
> packages are installed silently at whatever version satisfies `>=1.7.
|
|
28
|
+
> packages are installed silently at whatever version satisfies `>=1.7.16 <1.8.0` — your build stays
|
|
29
29
|
> green while the version is unpinned and free to drift. Verify with `pnpm why better-auth`.
|
|
30
30
|
|
|
31
31
|
**Why peers rather than dependencies:** in a fullstack project the frontend talks to better-auth
|
|
@@ -123,6 +123,45 @@ export class CoreBetterAuthApiMiddleware implements NestMiddleware {
|
|
|
123
123
|
return normalized;
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
/**
|
|
127
|
+
* `POST /revoke-session` with `{ sessionId }` (11.43.8): `list-sessions` hands out session ids and no tokens, so a
|
|
128
|
+
* client names the session to end by its id. Better-Auth's route takes `{ token }` only; this looks the token up among
|
|
129
|
+
* the sessions of the signed-in user ({@link CoreBetterAuthService.sessionTokenOfUser}) and puts it into the body.
|
|
130
|
+
*
|
|
131
|
+
* An id that is unknown or belongs to another user becomes an empty token: Better-Auth then revokes nothing and
|
|
132
|
+
* answers as it does for a foreign token, so the answer says nothing about other users' sessions. A body that
|
|
133
|
+
* already carries a `token` is left as it is (revoking by token keeps working).
|
|
134
|
+
*/
|
|
135
|
+
protected async resolveRevokeSessionId(req: Request, relativePath: string): Promise<void> {
|
|
136
|
+
if (req.method !== 'POST' || relativePath.replace(/\/+$/, '').toLowerCase() !== '/revoke-session') {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const body = req.body as Record<string, unknown> | undefined;
|
|
140
|
+
const sessionId = body?.sessionId;
|
|
141
|
+
if (!body || typeof sessionId !== 'string' || !sessionId || (typeof body.token === 'string' && body.token)) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const caller = req as unknown as {
|
|
145
|
+
betterAuthSession?: { session?: { userId?: unknown }; user?: { id?: unknown } };
|
|
146
|
+
betterAuthUser?: { id?: unknown };
|
|
147
|
+
};
|
|
148
|
+
// The id the sessions are stored under, from the caller's own session (11.43.9, DEV-4133). `user.id` is not it for
|
|
149
|
+
// every user: the session lookup prefers a STORED `id`, and the legacy migration (`migrateAccountToIam()`) writes
|
|
150
|
+
// one of its own next to `_id`, which is what every session references — so a migrated user's sessions were never
|
|
151
|
+
// found and nothing was revoked. The ownership check stays in sessionTokenOfUser().
|
|
152
|
+
const sessionUserId = caller.betterAuthSession?.session?.userId;
|
|
153
|
+
const userId =
|
|
154
|
+
(typeof sessionUserId === 'string' && sessionUserId ? sessionUserId : undefined) ??
|
|
155
|
+
caller.betterAuthSession?.user?.id ??
|
|
156
|
+
caller.betterAuthUser?.id;
|
|
157
|
+
const token =
|
|
158
|
+
typeof userId === 'string' && userId
|
|
159
|
+
? await this.betterAuthService.sessionTokenOfUser(userId, sessionId)
|
|
160
|
+
: undefined;
|
|
161
|
+
const { sessionId: _sessionId, ...rest } = body;
|
|
162
|
+
req.body = { ...rest, token: token ?? '' };
|
|
163
|
+
}
|
|
164
|
+
|
|
126
165
|
/**
|
|
127
166
|
* Enforces the configured password length on the value the CLIENT actually sent.
|
|
128
167
|
*
|
|
@@ -283,6 +322,8 @@ export class CoreBetterAuthApiMiddleware implements NestMiddleware {
|
|
|
283
322
|
// Put a reset password into the shape sign-in expects BEFORE the body is
|
|
284
323
|
// serialized, and keep the value for the legacy mirror below.
|
|
285
324
|
const resetPassword = this.normalizeResetPassword(req, relativePath);
|
|
325
|
+
// `list-sessions` hands out ids, no tokens: revoke-session takes the id (11.43.8)
|
|
326
|
+
await this.resolveRevokeSessionId(req, relativePath);
|
|
286
327
|
|
|
287
328
|
// Convert Express request to Web Standard Request with proper cookie signing
|
|
288
329
|
const webRequest = await toWebRequest(req, {
|
|
@@ -65,8 +65,11 @@ export function sessionTokenFromSetCookie(response: globalThis.Response, cookieN
|
|
|
65
65
|
* - Token in body: the session token becomes {@link SessionTokenPresentation.resolveJwt}'s answer; `session.token`
|
|
66
66
|
* leaves the body as in sign-in (`mapSession()`). A response that created a session without a top-level token
|
|
67
67
|
* (passkey verify answers `{ session }`) gains one.
|
|
68
|
-
* -
|
|
69
|
-
* `
|
|
68
|
+
* - Session records (an object with a `token`, a `userId` and an `expiresAt`) lose their `token` in every mode, also
|
|
69
|
+
* inside an array: `list-sessions` answered every session of the user with its token (11.43.8). The client revokes a
|
|
70
|
+
* session by its `id` instead (`POST /revoke-session` with `{ sessionId }`).
|
|
71
|
+
* - Untouched: error responses, non-JSON bodies, a JWT (`/token` hands one out on purpose), and a top-level `token`
|
|
72
|
+
* that is not the session token (another plugin's value).
|
|
70
73
|
*
|
|
71
74
|
* Returns the response itself when nothing changes, otherwise a new one with the same status and headers.
|
|
72
75
|
*/
|
|
@@ -83,9 +86,20 @@ export async function presentSessionTokens(
|
|
|
83
86
|
} catch {
|
|
84
87
|
return response;
|
|
85
88
|
}
|
|
86
|
-
if (!body || typeof body !== 'object'
|
|
89
|
+
if (!body || typeof body !== 'object') {
|
|
87
90
|
return response;
|
|
88
91
|
}
|
|
92
|
+
if (Array.isArray(body)) {
|
|
93
|
+
return body.some(isSessionRecord)
|
|
94
|
+
? rebuildResponse(
|
|
95
|
+
response,
|
|
96
|
+
body.map((item) => (isSessionRecord(item) ? withoutToken(item) : item)),
|
|
97
|
+
)
|
|
98
|
+
: response;
|
|
99
|
+
}
|
|
100
|
+
if (isSessionRecord(body)) {
|
|
101
|
+
return rebuildResponse(response, withoutToken(body));
|
|
102
|
+
}
|
|
89
103
|
|
|
90
104
|
const record = body as Record<string, unknown>;
|
|
91
105
|
const session =
|
|
@@ -129,6 +143,29 @@ export async function presentSessionTokens(
|
|
|
129
143
|
}
|
|
130
144
|
}
|
|
131
145
|
|
|
146
|
+
return rebuildResponse(response, next);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* A session as Better-Auth outputs it (`parseSessionOutput()`): its `token` is the bearer credential of that session,
|
|
151
|
+
* so it never belongs in a body — the `id` identifies the session to the client.
|
|
152
|
+
*/
|
|
153
|
+
function isSessionRecord(value: unknown): value is Record<string, unknown> {
|
|
154
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const record = value as Record<string, unknown>;
|
|
158
|
+
return typeof record.token === 'string' && !!record.token && 'userId' in record && 'expiresAt' in record;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The record without its `token` */
|
|
162
|
+
function withoutToken(record: Record<string, unknown>): Record<string, unknown> {
|
|
163
|
+
const { token: _token, ...rest } = record;
|
|
164
|
+
return rest;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** The same response with another JSON body: status and headers kept, every `Set-Cookie` re-appended */
|
|
168
|
+
function rebuildResponse(response: globalThis.Response, body: unknown): globalThis.Response {
|
|
132
169
|
const headers = new Headers();
|
|
133
170
|
response.headers.forEach((value, key) => {
|
|
134
171
|
if (key.toLowerCase() !== 'content-length' && key.toLowerCase() !== 'set-cookie') {
|
|
@@ -138,7 +175,7 @@ export async function presentSessionTokens(
|
|
|
138
175
|
for (const cookie of response.headers.getSetCookie?.() ?? []) {
|
|
139
176
|
headers.append('set-cookie', cookie);
|
|
140
177
|
}
|
|
141
|
-
return new Response(JSON.stringify(
|
|
178
|
+
return new Response(JSON.stringify(body), { headers, status: response.status, statusText: response.statusText });
|
|
142
179
|
}
|
|
143
180
|
|
|
144
181
|
/** Asks {@link SessionTokenPresentation.isSessionToken}; a lookup that fails cannot vouch for anything */
|
|
@@ -1038,6 +1038,35 @@ export class CoreBetterAuthService implements OnApplicationBootstrap, OnModuleIn
|
|
|
1038
1038
|
}
|
|
1039
1039
|
}
|
|
1040
1040
|
|
|
1041
|
+
/**
|
|
1042
|
+
* The token of the session `sessionId` — only when that session belongs to `userId` (11.43.8).
|
|
1043
|
+
*
|
|
1044
|
+
* `list-sessions` hands out session ids, no tokens, so a client revokes a session by its id; the API middleware asks
|
|
1045
|
+
* this to turn `POST /revoke-session { sessionId }` into the `{ token }` Better-Auth expects. Asked through
|
|
1046
|
+
* Better-Auth's own adapter (`internalAdapter.listSessions()`), so it reads wherever Better-Auth keeps its sessions.
|
|
1047
|
+
*
|
|
1048
|
+
* @returns the token, or `undefined` for an unknown id, a session of another user, or a failed lookup
|
|
1049
|
+
*/
|
|
1050
|
+
async sessionTokenOfUser(userId: string, sessionId: string): Promise<string | undefined> {
|
|
1051
|
+
if (!userId || !sessionId) {
|
|
1052
|
+
return undefined;
|
|
1053
|
+
}
|
|
1054
|
+
try {
|
|
1055
|
+
const context = await (this.getInstance() as { $context?: Promise<any> } | null)?.$context;
|
|
1056
|
+
// Active sessions only — the query Better-Auth's own list-sessions runs. Without the filter the adapter's default
|
|
1057
|
+
// cap (100 rows, oldest first) could hide every current session behind expired rows MongoDB never removes.
|
|
1058
|
+
const sessions: unknown = await context?.internalAdapter?.listSessions?.(userId, { onlyActiveSessions: true });
|
|
1059
|
+
const match = (Array.isArray(sessions) ? sessions : []).find(
|
|
1060
|
+
(session: { id?: unknown; userId?: unknown }) =>
|
|
1061
|
+
String(session?.id) === sessionId && String(session?.userId) === String(userId),
|
|
1062
|
+
);
|
|
1063
|
+
return typeof match?.token === 'string' && match.token ? match.token : undefined;
|
|
1064
|
+
} catch (error) {
|
|
1065
|
+
this.logger.debug(`sessionTokenOfUser error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
1066
|
+
return undefined;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1041
1070
|
/**
|
|
1042
1071
|
* Revokes a specific session (logout for that session)
|
|
1043
1072
|
*
|