@revenexx/cover 0.2.8 → 0.2.10

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.
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Resolve the signed-in user from the BFF for route middleware (SSR-safe).
3
+ *
4
+ * Why this forwards the brokered tenant headers, not just the cookie:
5
+ * on a deployed multi-tenant Site the platform entrypoint resolves the tenant
6
+ * from the request Host and injects `x-revenexx-tenant` + `x-revenexx-context`
7
+ * (the per-request gateway JWT) onto the incoming request. The BFF's SDK reads
8
+ * them off the request event (server/utils/revenexxSdk.ts `brokeredContext`)
9
+ * and there is NO env-credential fallback on a live Site.
10
+ *
11
+ * Route middleware resolves auth with an internal `$fetch("/api/auth/me")`.
12
+ * That internal request gets a fresh event that does NOT inherit the original
13
+ * navigation's headers — so forwarding only the cookie leaves auth/me with no
14
+ * tenant context: the SDK throws 503 "api_not_configured", me() returns null,
15
+ * and the guard bounces a signed-in visitor to /login on every hard SSR load
16
+ * (refresh, deep link, server-rendered first paint). Forwarding the brokered
17
+ * headers scopes the internal call to the same tenant as the navigation that
18
+ * triggered it. On the client useRequestHeaders() returns {} and the browser's
19
+ * own request already carries the cookie + hits the tenant-injecting edge, so
20
+ * the same call works there unchanged.
21
+ */
22
+ export async function fetchSessionUser(): Promise<{ $id: string } | null> {
23
+ const headers = useRequestHeaders(["cookie", "x-revenexx-tenant", "x-revenexx-context"]);
24
+ return await $fetch<{ $id: string } | null>("/api/auth/me", { headers }).catch(() => null);
25
+ }
@@ -6,12 +6,13 @@ export default defineNuxtRouteMiddleware(async (to) => {
6
6
  return;
7
7
  }
8
8
 
9
- // Resolve the session against the BFF like guest-only and checkout-guard
10
- // do. The auth store's user is not hydrated during SSR, so store-based
11
- // checks would bounce signed-in users to /login — and guest-only would
12
- // bounce them straight back, looping the request.
13
- const headers = useRequestHeaders(["cookie"]);
14
- const user = await $fetch<{ $id: string } | null>("/api/auth/me", { headers }).catch(() => null);
9
+ // Resolve the session against the BFF like guest-only does. The auth
10
+ // store's user is not hydrated during SSR, so store-based checks would
11
+ // bounce signed-in users to /login — and guest-only would bounce them
12
+ // straight back, looping the request. fetchSessionUser() forwards the
13
+ // brokered tenant headers so the internal call authenticates on a live
14
+ // multi-tenant Site (see useSessionUser.ts).
15
+ const user = await fetchSessionUser();
15
16
  if (user) {
16
17
  return;
17
18
  }
@@ -1,8 +1,9 @@
1
1
  export default defineNuxtRouteMiddleware(async () => {
2
2
  const localePath = useLocalePath();
3
3
 
4
- const headers = useRequestHeaders(["cookie"]);
5
- const user = await $fetch<{ $id: string } | null>("/api/auth/me", { headers }).catch(() => null);
4
+ // fetchSessionUser() forwards the brokered tenant headers so the internal
5
+ // auth/me call resolves on a live multi-tenant Site (see useSessionUser.ts).
6
+ const user = await fetchSessionUser();
6
7
  if (user) {
7
8
  return navigateTo(localePath("/account"));
8
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@revenexx/cover",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "description": "Cover — revenexx design system for Nuxt. Distributed as a Nuxt layer: generic UI components, theming tokens and stores shared by the demo shop, custom storefronts and the Blokkli theme.",
5
5
  "type": "module",
6
6
  "main": "./nuxt.config.ts",
@@ -91,42 +91,49 @@ export class ApiAuthService implements IAuthService {
91
91
  return null;
92
92
  }
93
93
 
94
- try {
95
- // session_id makes the customers app validate the session is still
96
- // alive — a logged-out/revoked session answers 401 here. Raw call:
97
- // the AuthMeRequest contract does not declare session_id yet, so
98
- // the generated customersAuthMe() would drop the session check.
99
- const { user, contact } = await useRevenexxSdk().call<{
100
- user: { $id: string; name?: string; email: string };
101
- contact: ApiContact | null;
102
- }>("POST", "/v1/customers/auth/me", { body: { user_id: parsed.userId, session_id: parsed.id } });
94
+ // Validate the session against the customers app, retrying transient
95
+ // failures. A 401 is the only proof the session is gone (revoked/expired)
96
+ // → clear the cookie + log out. Anything else (5xx "capability dispatch
97
+ // failed", network blip, cold-start dispatch) is transient infra trouble:
98
+ // returning null on the FIRST blip bounced a still-valid session to login
99
+ // mid-flight (worst on a cold storefront's first SSR me()). So retry a
100
+ // couple times before giving up; the cookie is never dropped on transient
101
+ // errors, and the request only degrades to guest if every attempt blips.
102
+ const MAX_ATTEMPTS = 3;
103
+ let lastErr: unknown;
104
+ for (let attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
105
+ try {
106
+ // session_id makes the customers app validate the session is still
107
+ // alive — a logged-out/revoked session answers 401 here. Raw call:
108
+ // the AuthMeRequest contract does not declare session_id yet, so
109
+ // the generated customersAuthMe() would drop the session check.
110
+ const { user, contact } = await useRevenexxSdk().call<{
111
+ user: { $id: string; name?: string; email: string };
112
+ contact: ApiContact | null;
113
+ }>("POST", "/v1/customers/auth/me", { body: { user_id: parsed.userId, session_id: parsed.id } });
103
114
 
104
- return {
105
- $id: user.$id,
106
- name: contactName(contact, user.name ?? user.email),
107
- email: user.email,
108
- role: toAuthRole(contact?.role),
109
- ...(contact?.id ? { contactId: contact.id } : {}),
110
- ...(contact?.organization_id ? { organizationId: contact.organization_id } : {}),
111
- };
112
- }
113
- catch (err) {
114
- // Only a genuine auth failure (401 = revoked/expired session) proves
115
- // the session is gone — clear the cookie so the user logs out cleanly.
116
- // Any other error (5xx like the gateway's "capability dispatch failed",
117
- // or a network blip) is transient infrastructure trouble, NOT proof the
118
- // session is invalid. Deleting the cookie here turned a flaky gateway
119
- // into a SILENT LOGOUT + empty order history (the account history is
120
- // filtered strictly by contact_id and falls back to me()). So keep the
121
- // cookie and return null: callers still degrade to the guest path for
122
- // this one request, and the session self-heals on the next.
123
- if (isApiUserError(err) && err.code === 401) {
124
- deleteCookie(event, SESSION_COOKIE_NAME, sessionCookieOptions());
125
- return null;
115
+ return {
116
+ $id: user.$id,
117
+ name: contactName(contact, user.name ?? user.email),
118
+ email: user.email,
119
+ role: toAuthRole(contact?.role),
120
+ ...(contact?.id ? { contactId: contact.id } : {}),
121
+ ...(contact?.organization_id ? { organizationId: contact.organization_id } : {}),
122
+ };
123
+ }
124
+ catch (err) {
125
+ lastErr = err;
126
+ if (isApiUserError(err) && err.code === 401) {
127
+ deleteCookie(event, SESSION_COOKIE_NAME, sessionCookieOptions());
128
+ return null;
129
+ }
130
+ if (attempt < MAX_ATTEMPTS) {
131
+ await new Promise(resolve => setTimeout(resolve, 300 * attempt));
132
+ }
126
133
  }
127
- getLogService().error("API request failed: auth/me", apiErrorContext(err));
128
- return null;
129
134
  }
135
+ getLogService().error("API request failed: auth/me", apiErrorContext(lastErr));
136
+ return null;
130
137
  }
131
138
 
132
139
  async logout(event: H3Event): Promise<void> {