@jxsuite/auth 0.8.8 → 0.8.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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/worker.ts +30 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jxsuite/auth",
3
- "version": "0.8.8",
3
+ "version": "0.8.9",
4
4
  "description": "Better Auth sessions, sign-in flows, and table permissions for Jx sites over /_jx/auth",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,8 +38,8 @@
38
38
  "upgrade": "bun update --latest && bun install"
39
39
  },
40
40
  "dependencies": {
41
- "@jxsuite/connector": "^0.8.8",
42
- "@jxsuite/schema": "^5.6.0",
41
+ "@jxsuite/connector": "^0.8.9",
42
+ "@jxsuite/schema": "^6.0.0",
43
43
  "better-auth": "^1.7.15",
44
44
  "kysely": "^0.29.8"
45
45
  },
package/src/worker.ts CHANGED
@@ -99,13 +99,40 @@ export async function handleAuthRequest(
99
99
  ): Promise<Response> {
100
100
  try {
101
101
  const auth = await getAuthForEnv(state, options, env);
102
- return await auth.handler(request);
102
+ const response = await auth.handler(request);
103
+ return isSignOut(request) && response.ok ? clearingSiteData(response) : response;
103
104
  } catch (error) {
104
- const message = error instanceof Error ? error.message : String(error);
105
- return Response.json({ error: message }, { status: 500 });
105
+ /* The cause names a secret's env var, a connection or a driver. It is the owner's to read in
106
+ the log; a visitor is told only that sign-in is not working. */
107
+ console.error("jx auth: request failed:", error);
108
+ return Response.json({ error: "Authentication is unavailable" }, { status: 500 });
106
109
  }
107
110
  }
108
111
 
112
+ /** Whether a request is the sign-out call, whatever base the site is deployed under. */
113
+ function isSignOut(request: Request): boolean {
114
+ return request.method === "POST" && new URL(request.url).pathname.endsWith("/sign-out");
115
+ }
116
+
117
+ /**
118
+ * The same response, asking the browser to drop what it cached for this origin.
119
+ *
120
+ * Better Auth expires the session cookie, which ends the session and leaves every page the person
121
+ * saw while signed in one Back press away: in the HTTP cache, and in the back/forward cache, which
122
+ * restores a page without asking the server anything. `"cache"` empties both. `"storage"` is
123
+ * deliberately not sent: it would also delete the site's own localStorage (a theme, a cart) and
124
+ * unregister its service worker, which is the site's decision and not this mount's.
125
+ */
126
+ function clearingSiteData(response: Response): Response {
127
+ const headers = new Headers(response.headers);
128
+ headers.set("Clear-Site-Data", '"cache"');
129
+ return new Response(response.body, {
130
+ headers,
131
+ status: response.status,
132
+ statusText: response.statusText,
133
+ });
134
+ }
135
+
109
136
  /** The mount + section-owner class for the `auth` section. */
110
137
  export const Auth = {
111
138
  /**