@lenne.tech/nuxt-extensions 4.0.5 → 4.0.7
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.md +28 -0
- package/README.md +16 -0
- package/dist/module.d.mts +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/composables/auth/use-lt-auth.js +42 -1
- package/dist/runtime/lib/auth-state.d.ts +4 -0
- package/dist/runtime/lib/auth-state.js +7 -0
- package/dist/runtime/plugins/auth-interceptor.client.js +2 -2
- package/dist/runtime/types/auth.d.ts +14 -0
- package/package.json +4 -4
package/CLAUDE.md
CHANGED
|
@@ -303,6 +303,34 @@ unchanged. That token is the session token, the value the API keeps in the httpO
|
|
|
303
303
|
`'cookie'` that left JWT mode, and the JWT the client had just stored was no longer sent: the
|
|
304
304
|
app then depended on a session cookie, which JWT mode exists not to rely on. Never let another
|
|
305
305
|
user keep JWT mode: the stored JWT is the previous user's.
|
|
306
|
+
- **`validateSession()` keeps the mode for the signed-in user while a JWT is stored** (since
|
|
307
|
+
4.0.6; ng-base does the same since 22.0.0). Up to 4.0.5 it set `'cookie'` and fetched a new JWT,
|
|
308
|
+
so after every page load in JWT mode requests went out without the bearer until `/iam/token`
|
|
309
|
+
answered; the starter's E2E measured `two-factor/disable` without an `Authorization` header.
|
|
310
|
+
With `cookies: false` those requests, and the cookie-only `/iam/token` itself, then worked only
|
|
311
|
+
because nest-server reads the JWT cookie when no bearer comes, and before 12.11.2 it ignored
|
|
312
|
+
`COOKIE_PREFIX` there. Another user, or JWT mode without a stored JWT, starts on `'cookie'`.
|
|
313
|
+
**The `/iam/token` request on every page load stays, in JWT mode too.** With `cookies: false`
|
|
314
|
+
nest-server sets no session cookie, so `/iam/token` can only be paid for by a JWT cookie that is
|
|
315
|
+
still valid, and an expired JWT cannot be renewed at all; ng-base renews before sending
|
|
316
|
+
(`currentJwt()`), this module has nothing like it. Asking `/iam/token` only in cookie mode was
|
|
317
|
+
tried during 4.0.6 and ended every such sign-in with its first JWT, however often the page was
|
|
318
|
+
loaded. `test/validate-session-mode.test.ts` holds this.
|
|
319
|
+
- **`validateSession()` signs out a cached user the API has no session for** (since 4.0.6): a
|
|
320
|
+
`get-session` that ANSWERED without a session (not pending, no `error`), outside JWT mode with a
|
|
321
|
+
stored JWT, and confirmed by a direct `get-session` (`probeSession()`, the 401 interceptor's
|
|
322
|
+
rules: 401/403 or an empty 200 is dead, 5xx/429/network decides nothing) → `clearUser()`,
|
|
323
|
+
`false`. Up to 4.0.5 the cached user was trusted there: a project's API session had expired, the
|
|
324
|
+
page greeted the user by name, and every protected call failed (its API answered 403, so the
|
|
325
|
+
interceptor never fired either). **Never drop the confirming probe**: Better Auth's session atom
|
|
326
|
+
can hold a stale empty answer after a sign-in that does not refetch it, and a page load must not
|
|
327
|
+
undo a sign-in. `clearUser()`, not `signOut()`: an expired session deletes no offline-sync data.
|
|
328
|
+
**The sign-out keeps the redirect** (since 4.0.7): it sets `setLtSignedOutAtValidation(true)`
|
|
329
|
+
(`lib/auth-state.ts`, internal), and the interceptor handles a 401 without a signed-in user when
|
|
330
|
+
that note is set, so the next 401 confirms the dead session and goes to the login page as it did
|
|
331
|
+
for the cached user. In 4.0.6 the guard `if (!isAuthenticated) return` swallowed it, and the
|
|
332
|
+
visitor stayed on the protected page, shown as signed out, with every request failing (ng-base
|
|
333
|
+
found the same gap in 22.1.4). `setUser()` and `clearUser()` reset the note.
|
|
306
334
|
- **`useLtAuthClient().twoFactor.verify*` / `enable` / `disable`, `changePassword` and
|
|
307
335
|
`signIn.social` strip a non-JWT `token` from the answer**, and `authenticateWithPasskey()`
|
|
308
336
|
returns no `session`. Keep all of them: application code that never receives the session token
|
package/README.md
CHANGED
|
@@ -551,6 +551,22 @@ or `exposeTokenInBody`): the JWT of the session that is valid after the answer.
|
|
|
551
551
|
(same `id`), as `setUser({ ...user.value, twoFactorEnabled: true })` after turning 2FA on does.
|
|
552
552
|
Any other user starts on the cookie session (since 4.0.5; before, every call without a mode
|
|
553
553
|
meant cookie mode, and a settings page in JWT mode stopped sending its JWT).
|
|
554
|
+
- **`validateSession()` keeps the auth mode of the signed-in user** (since 4.0.6). After a page
|
|
555
|
+
load in JWT mode the stored JWT goes out with the first request, while a new one is fetched from
|
|
556
|
+
`/iam/token` in the background. Up to 4.0.5 every page load switched to cookie mode until
|
|
557
|
+
`/iam/token` answered, and requests in that window went out without the JWT. Another user, or
|
|
558
|
+
JWT mode with no JWT stored, starts on the cookie session.
|
|
559
|
+
- **`validateSession()` signs out a user the API no longer has a session for** (since 4.0.6).
|
|
560
|
+
When `get-session` answers without a session while `lt-auth-state` still holds a user (the
|
|
561
|
+
session expired or was deleted on the server), it calls `clearUser()` and returns `false`, after
|
|
562
|
+
a second, direct `get-session` confirmed it. Up to 4.0.5 it returned `true` for the cached user:
|
|
563
|
+
the page showed the user as signed in while every protected request failed, until one of them
|
|
564
|
+
answered 401. JWT mode with a stored JWT keeps the user, and so does an answer that is not a
|
|
565
|
+
verdict (a timeout, a network error, a 5xx). Unsent offline-sync changes stay, as after any
|
|
566
|
+
expired session.
|
|
567
|
+
The visitor can still be on a protected page then: the next request that answers 401 takes them
|
|
568
|
+
to the login page, as it did for the cached user (since 4.0.7; in 4.0.6 that 401 was ignored,
|
|
569
|
+
because nobody was signed in any more).
|
|
554
570
|
- **`useLtAuthClient().twoFactor.verifyTotp()`, `verifyBackupCode()`, `enable()`, `disable()`
|
|
555
571
|
and `changePassword()` return no session token.** A JWT in the answer stays, and the client
|
|
556
572
|
stores it in `lt-jwt-token` (since 4.0.5); the auth mode is left as it is. From nest-server
|
package/dist/module.d.mts
CHANGED
|
@@ -3,7 +3,7 @@ import { LtExtensionsModuleOptions } from '../dist/runtime/types/index.js';
|
|
|
3
3
|
export * from '../dist/runtime/types/index.js';
|
|
4
4
|
|
|
5
5
|
declare const name = "@lenne.tech/nuxt-extensions";
|
|
6
|
-
declare const version = "4.0.
|
|
6
|
+
declare const version = "4.0.7";
|
|
7
7
|
declare const configKey = "ltExtensions";
|
|
8
8
|
declare const DEFAULT_LT_AUTH_STATE_COOKIE = "lt-auth-state";
|
|
9
9
|
declare const DEFAULT_LT_JWT_TOKEN_COOKIE = "lt-jwt-token";
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { pathToFileURL } from 'node:url';
|
|
|
3
3
|
import { SECURITY_TXT_DEFAULT_VALID_DAYS, SECURITY_TXT_DEFAULT_LANGUAGES, SECURITY_TXT_DEFAULT_CONTACT, SECURITY_TXT_PATH } from '../dist/runtime/server/lib/security-txt.js';
|
|
4
4
|
|
|
5
5
|
const name = "@lenne.tech/nuxt-extensions";
|
|
6
|
-
const version = "4.0.
|
|
6
|
+
const version = "4.0.7";
|
|
7
7
|
const configKey = "ltExtensions";
|
|
8
8
|
const DEFAULT_LT_AUTH_STATE_COOKIE = "lt-auth-state";
|
|
9
9
|
const DEFAULT_LT_JWT_TOKEN_COOKIE = "lt-jwt-token";
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
noteLtJwtServedByPayload,
|
|
12
12
|
requestLtJwt,
|
|
13
13
|
resolveLtAuthState,
|
|
14
|
+
setLtSignedOutAtValidation,
|
|
14
15
|
toLtStoredAuthState,
|
|
15
16
|
warnLtAdminPluginUnserved
|
|
16
17
|
} from "../../lib/auth-state.js";
|
|
@@ -108,6 +109,7 @@ export function useLtAuth() {
|
|
|
108
109
|
authState.value = newState;
|
|
109
110
|
}
|
|
110
111
|
if (import.meta.client) {
|
|
112
|
+
setLtSignedOutAtValidation(false);
|
|
111
113
|
const maxAge = 60 * 60 * 24 * 7;
|
|
112
114
|
const secure = globalThis.location?.protocol === "https:" ? "; secure" : "";
|
|
113
115
|
const encoded = encodeURIComponent(JSON.stringify(newState));
|
|
@@ -123,6 +125,7 @@ export function useLtAuth() {
|
|
|
123
125
|
if (import.meta.client) {
|
|
124
126
|
authState.value = { user: null, authMode: "cookie" };
|
|
125
127
|
jwtToken.value = null;
|
|
128
|
+
setLtSignedOutAtValidation(false);
|
|
126
129
|
clearLtAuthCookies();
|
|
127
130
|
}
|
|
128
131
|
}
|
|
@@ -169,6 +172,36 @@ export function useLtAuth() {
|
|
|
169
172
|
}
|
|
170
173
|
return merged;
|
|
171
174
|
}
|
|
175
|
+
async function probeSession() {
|
|
176
|
+
const controller = new AbortController();
|
|
177
|
+
let timer;
|
|
178
|
+
const timeout = new Promise((resolve) => {
|
|
179
|
+
timer = setTimeout(() => {
|
|
180
|
+
controller.abort();
|
|
181
|
+
resolve(null);
|
|
182
|
+
}, VALIDATE_SESSION_TIMEOUT_MS);
|
|
183
|
+
});
|
|
184
|
+
const verdict = (async () => {
|
|
185
|
+
try {
|
|
186
|
+
const response = await fetchWithAuth(`${getLtApiBase()}/get-session`, { method: "GET", signal: controller.signal });
|
|
187
|
+
if (response.status === 401 || response.status === 403) {
|
|
188
|
+
return false;
|
|
189
|
+
}
|
|
190
|
+
if (!response.ok) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const data = await response.json().catch(() => null);
|
|
194
|
+
return Boolean(data && (data.user || data.session));
|
|
195
|
+
} catch {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
})();
|
|
199
|
+
try {
|
|
200
|
+
return await Promise.race([verdict, timeout]);
|
|
201
|
+
} finally {
|
|
202
|
+
clearTimeout(timer);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
172
205
|
async function validateSession() {
|
|
173
206
|
if (import.meta.server) {
|
|
174
207
|
return !!resolvedAuthState.value?.user;
|
|
@@ -196,12 +229,20 @@ export function useLtAuth() {
|
|
|
196
229
|
});
|
|
197
230
|
}
|
|
198
231
|
if (session.value.data?.user) {
|
|
199
|
-
|
|
232
|
+
const sessionUser = mergeSessionUser(session.value.data.user);
|
|
233
|
+
const keepsJwt = isJwtMode.value && !!jwtToken.value && user.value?.id === sessionUser.id;
|
|
234
|
+
setUser(sessionUser, keepsJwt ? "jwt" : "cookie");
|
|
200
235
|
switchToJwtMode().catch(() => {
|
|
201
236
|
});
|
|
202
237
|
return true;
|
|
203
238
|
}
|
|
204
239
|
if (resolvedAuthState.value?.user) {
|
|
240
|
+
const answeredWithoutSession = !session.value.isPending && !session.value.error;
|
|
241
|
+
if (answeredWithoutSession && !(isJwtMode.value && jwtToken.value) && await probeSession() === false) {
|
|
242
|
+
clearUser();
|
|
243
|
+
setLtSignedOutAtValidation(true);
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
205
246
|
switchToJwtMode().catch(() => {
|
|
206
247
|
});
|
|
207
248
|
return true;
|
|
@@ -427,6 +427,10 @@ export interface LtAuthFetchOptions {
|
|
|
427
427
|
cookiesInJwtMode?: 'always' | 'auth-paths';
|
|
428
428
|
state: LtAuthFetchState;
|
|
429
429
|
}
|
|
430
|
+
/** @internal See {@link ltSignedOutAtValidation}. */
|
|
431
|
+
export declare function setLtSignedOutAtValidation(value: boolean): void;
|
|
432
|
+
/** @internal See {@link ltSignedOutAtValidation}. */
|
|
433
|
+
export declare function isLtSignedOutAtValidation(): boolean;
|
|
430
434
|
/**
|
|
431
435
|
* The authenticated fetch, one implementation for every caller.
|
|
432
436
|
*
|
|
@@ -330,6 +330,13 @@ const ltCookieAuthFetchState = {
|
|
|
330
330
|
jwtToken: getLtJwtToken,
|
|
331
331
|
mode: getLtAuthMode
|
|
332
332
|
};
|
|
333
|
+
let ltSignedOutAtValidation = false;
|
|
334
|
+
export function setLtSignedOutAtValidation(value) {
|
|
335
|
+
ltSignedOutAtValidation = value;
|
|
336
|
+
}
|
|
337
|
+
export function isLtSignedOutAtValidation() {
|
|
338
|
+
return ltSignedOutAtValidation;
|
|
339
|
+
}
|
|
333
340
|
let ltJwtAfter401 = null;
|
|
334
341
|
function requestLtJwtAfter401(basePath) {
|
|
335
342
|
if (!import.meta.client) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defineNuxtPlugin } from "#imports";
|
|
2
2
|
import { useLtAuth } from "../composables/auth/use-lt-auth.js";
|
|
3
|
-
import { getLtApiBase } from "../lib/auth-state.js";
|
|
3
|
+
import { getLtApiBase, isLtSignedOutAtValidation } from "../lib/auth-state.js";
|
|
4
4
|
export default defineNuxtPlugin((nuxtApp) => {
|
|
5
5
|
if (import.meta.server) return;
|
|
6
6
|
let _authInstance = null;
|
|
@@ -97,7 +97,7 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|
|
97
97
|
return;
|
|
98
98
|
}
|
|
99
99
|
const { clearUser, isAuthenticated } = getAuth();
|
|
100
|
-
if (!isAuthenticated.value) {
|
|
100
|
+
if (!isAuthenticated.value && !isLtSignedOutAtValidation()) {
|
|
101
101
|
return;
|
|
102
102
|
}
|
|
103
103
|
isHandling401 = true;
|
|
@@ -367,6 +367,20 @@ export interface UseLtAuthReturn {
|
|
|
367
367
|
}>(params: NoStrayCredential<T, 'password'>, options?: unknown) => Promise<unknown>;
|
|
368
368
|
};
|
|
369
369
|
switchToJwtMode: () => Promise<boolean>;
|
|
370
|
+
/**
|
|
371
|
+
* Re-validates the session (app start, hard reload) through `get-session` and merges the
|
|
372
|
+
* session user onto the cached one. `true` while signed in.
|
|
373
|
+
*
|
|
374
|
+
* The signed-in user keeps JWT mode while a JWT is stored, and the stored JWT is sent while a
|
|
375
|
+
* new one is fetched in the background (since 4.0.6; before, every page load switched to cookie
|
|
376
|
+
* mode until the new JWT came). Another user, or JWT mode without a stored JWT, starts on the
|
|
377
|
+
* cookie session.
|
|
378
|
+
*
|
|
379
|
+
* When the API has no session for the cached user any more (confirmed by a direct
|
|
380
|
+
* `get-session`), the user is cleared and the result is `false` (since 4.0.6; before, the cached
|
|
381
|
+
* user counted as signed in). JWT mode with a stored JWT, a timeout and a failed request keep
|
|
382
|
+
* the user.
|
|
383
|
+
*/
|
|
370
384
|
validateSession: () => Promise<boolean>;
|
|
371
385
|
passkey?: unknown;
|
|
372
386
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lenne.tech/nuxt-extensions",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7",
|
|
4
4
|
"description": "Reusable Nuxt 4 composables, components, and Better-Auth integration for lenne.tech projects",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -112,7 +112,7 @@
|
|
|
112
112
|
"devDependencies": {
|
|
113
113
|
"@better-auth/passkey": "1.7.19",
|
|
114
114
|
"@nuxt/module-builder": "1.0.4",
|
|
115
|
-
"@nuxt/schema": "4.
|
|
115
|
+
"@nuxt/schema": "4.7.0",
|
|
116
116
|
"@nuxt/ui": "4.13.1",
|
|
117
117
|
"@playwright/test": "1.65.1",
|
|
118
118
|
"@types/node": "26.6.6",
|
|
@@ -122,7 +122,7 @@
|
|
|
122
122
|
"cross-spawn": "7.0.6",
|
|
123
123
|
"fake-indexeddb": "6.2.8",
|
|
124
124
|
"happy-dom": "21.0.4",
|
|
125
|
-
"nuxt": "4.
|
|
125
|
+
"nuxt": "4.7.0",
|
|
126
126
|
"oxfmt": "0.80.0",
|
|
127
127
|
"oxlint": "1.95.0",
|
|
128
128
|
"oxlint-tsgolint": "7.0.2008",
|
|
@@ -151,5 +151,5 @@
|
|
|
151
151
|
"node": ">= 22",
|
|
152
152
|
"pnpm": "^11.0.0"
|
|
153
153
|
},
|
|
154
|
-
"packageManager": "pnpm@11.
|
|
154
|
+
"packageManager": "pnpm@11.29.0+sha512.d913f9b37cd5fe6780052e809774edabb7bdcf69a6e6d9b8fd3b38bc052fa8a7d891b6c201a38fb504a07ffce2e5e8c262ef54fb8f57db62f22d353a30bc8a96"
|
|
155
155
|
}
|