@7365admin1/layer-common 4.21.4-staging.343 → 4.21.4-staging.345

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@7365admin1/layer-common",
3
3
  "license": "MIT",
4
4
  "type": "module",
5
- "version": "4.21.4-staging.343",
5
+ "version": "4.21.4-staging.345",
6
6
  "author": "7365admin1",
7
7
  "main": "./nuxt.config.ts",
8
8
  "//files": "What a consumer extending this layer actually loads. Without this npm ships the whole working tree - the changesets, the CI workflows, the render harness in tools/ and any scratch directory that happened to exist at publish time. Nuxt resolves a layer by directory, so every runtime directory below has to stay listed; adding a new top-level runtime directory means adding it here too.",
package/plugins/API.ts CHANGED
@@ -1,4 +1,4 @@
1
- export default defineNuxtPlugin(() => {
1
+ export default defineNuxtPlugin((nuxtApp) => {
2
2
  const { cookieConfig } = useRuntimeConfig().public;
3
3
 
4
4
  const api = $fetch.create({
@@ -10,6 +10,21 @@ export default defineNuxtPlugin(() => {
10
10
  const sid = useCookie("sid", cookieConfig).value ?? "";
11
11
  options.headers.set("Authorization", sid);
12
12
  },
13
+ // "You do not have permission to ..." - the role changed since the page
14
+ // read it. Re-read it in the background (plugins/secure-member.client.ts)
15
+ // so the screen stops offering what the server refuses. The request itself
16
+ // still fails as before; nothing here retries it.
17
+ onResponseError({ response }) {
18
+ if (response?.status !== 403) return;
19
+ if ((response as any)?._data?.code !== "PERMISSION_DENIED") return;
20
+ if (!import.meta.client) return;
21
+ const refresh = (nuxtApp as any).$refreshAppRole;
22
+ if (typeof refresh === "function") {
23
+ // Outside a component the Nuxt context is gone by now; run in it so the
24
+ // refresh can read the session cookie.
25
+ void nuxtApp.runWithContext(() => refresh());
26
+ }
27
+ },
13
28
  });
14
29
 
15
30
  // Expose to useNuxtApp().$api
@@ -240,4 +240,48 @@ export default defineNuxtPlugin(() => {
240
240
  // Cosmetic only, and it never blocked navigation before either.
241
241
  }
242
242
  });
243
+
244
+ /*
245
+ * PERMISSIONS CHANGED WHILE THE PAGE IS OPEN.
246
+ *
247
+ * The role is read on page load and on a site change. If an administrator
248
+ * unticks a right afterwards, the screen still offers it and the SERVER
249
+ * refuses: 403 with `code: "PERMISSION_DENIED"`. `plugins/API.ts` calls this
250
+ * on that refusal - the role is read again in the background, the same way
251
+ * the page load reads it, so menus, tabs and buttons catch up without a
252
+ * reload. Single-flight, and at most once every few seconds, so a burst of
253
+ * refusals makes one refresh and nothing can loop. A failed read keeps the
254
+ * role on screen (see `run`).
255
+ */
256
+ let refreshing: Promise<void> | null = null;
257
+ let lastRefreshAt = 0;
258
+ const REFRESH_COOLDOWN_MS = 5000;
259
+
260
+ function refreshAppRole(): Promise<void> {
261
+ const userId = useCookie("user").value ?? "";
262
+ if (!userId) return Promise.resolve();
263
+ if (refreshing) return refreshing;
264
+ if (inFlight || Date.now() - lastRefreshAt < REFRESH_COOLDOWN_MS) {
265
+ return Promise.resolve();
266
+ }
267
+ lastRefreshAt = Date.now();
268
+ const site = idParam(router.currentRoute.value.params.site);
269
+
270
+ refreshing = (async () => {
271
+ await run(userId, site);
272
+ deniedFor = null;
273
+ await resolveDenied(userId, site);
274
+ })()
275
+ .catch((error) => console.warn("[permissions] refresh after 403 failed", error))
276
+ .finally(() => {
277
+ refreshing = null;
278
+ });
279
+ return refreshing;
280
+ }
281
+
282
+ return {
283
+ provide: {
284
+ refreshAppRole,
285
+ },
286
+ };
243
287
  });