@hono/auth-js 1.0.1 → 1.0.3

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/dist/index.js CHANGED
@@ -33,6 +33,7 @@ function reqWithEnvUrl(req, authUrl) {
33
33
  }
34
34
  function setEnvDefaults(env, config) {
35
35
  config.secret ??= env.AUTH_SECRET;
36
+ config.basePath ??= "/api/auth";
36
37
  config.trustHost = true;
37
38
  config.redirectProxyUrl ??= env.AUTH_REDIRECT_PROXY_URL;
38
39
  config.providers = config.providers.map((p) => {
@@ -50,11 +51,11 @@ function setEnvDefaults(env, config) {
50
51
  }
51
52
  async function getAuthUser(c) {
52
53
  const config = c.get("authConfig");
54
+ setEnvDefaults(c.env, config);
53
55
  const origin = new URL(c.req.url, c.env.AUTH_URL).origin;
54
- const request = new Request(`${origin}/session`, {
56
+ const request = new Request(`${origin}${config.basePath}/session`, {
55
57
  headers: { cookie: c.req.header("cookie") ?? "" }
56
58
  });
57
- setEnvDefaults(c.env, config);
58
59
  let authUser = {};
59
60
  const response = await (0, import_core.Auth)(request, {
60
61
  ...config,
@@ -80,8 +81,9 @@ function verifyAuth() {
80
81
  status: 401
81
82
  });
82
83
  throw new import_http_exception.HTTPException(401, { res });
83
- } else
84
+ } else {
84
85
  c.set("authUser", authUser);
86
+ }
85
87
  await next();
86
88
  };
87
89
  }
package/dist/index.mjs CHANGED
@@ -6,6 +6,7 @@ function reqWithEnvUrl(req, authUrl) {
6
6
  }
7
7
  function setEnvDefaults(env, config) {
8
8
  config.secret ??= env.AUTH_SECRET;
9
+ config.basePath ??= "/api/auth";
9
10
  config.trustHost = true;
10
11
  config.redirectProxyUrl ??= env.AUTH_REDIRECT_PROXY_URL;
11
12
  config.providers = config.providers.map((p) => {
@@ -23,11 +24,11 @@ function setEnvDefaults(env, config) {
23
24
  }
24
25
  async function getAuthUser(c) {
25
26
  const config = c.get("authConfig");
27
+ setEnvDefaults(c.env, config);
26
28
  const origin = new URL(c.req.url, c.env.AUTH_URL).origin;
27
- const request = new Request(`${origin}/session`, {
29
+ const request = new Request(`${origin}${config.basePath}/session`, {
28
30
  headers: { cookie: c.req.header("cookie") ?? "" }
29
31
  });
30
- setEnvDefaults(c.env, config);
31
32
  let authUser = {};
32
33
  const response = await Auth(request, {
33
34
  ...config,
@@ -53,8 +54,9 @@ function verifyAuth() {
53
54
  status: 401
54
55
  });
55
56
  throw new HTTPException(401, { res });
56
- } else
57
+ } else {
57
58
  c.set("authUser", authUser);
59
+ }
58
60
  await next();
59
61
  };
60
62
  }
package/dist/react.js CHANGED
@@ -66,8 +66,9 @@ async function fetchData(path, config, logger2, req = {}) {
66
66
  }
67
67
  const res = await fetch(url, options);
68
68
  const data = await res.json();
69
- if (!res.ok)
69
+ if (!res.ok) {
70
70
  throw data;
71
+ }
71
72
  return data;
72
73
  } catch (error) {
73
74
  logger2.error(new ClientFetchError(error.message, error));
@@ -169,10 +170,11 @@ function useSession(options) {
169
170
  error: "SessionRequired",
170
171
  callbackUrl: window.location.href
171
172
  })}`;
172
- if (onUnauthenticated)
173
+ if (onUnauthenticated) {
173
174
  onUnauthenticated();
174
- else
175
+ } else {
175
176
  window.location.href = url;
177
+ }
176
178
  }
177
179
  }, [requiredAndNotLoading, onUnauthenticated]);
178
180
  if (requiredAndNotLoading) {
@@ -195,7 +197,11 @@ async function getSession(params) {
195
197
  return session;
196
198
  }
197
199
  async function getCsrfToken() {
198
- const response = await fetchData("csrf", authConfigManager.getConfig(), logger);
200
+ const response = await fetchData(
201
+ "csrf",
202
+ authConfigManager.getConfig(),
203
+ logger
204
+ );
199
205
  return response?.csrfToken ?? "";
200
206
  }
201
207
  async function getProviders() {
@@ -235,8 +241,9 @@ async function signIn(provider, options, authorizationParams) {
235
241
  if (redirect || !isSupportingReturn) {
236
242
  const url = data.url ?? callbackUrl;
237
243
  window.location.href = url;
238
- if (url.includes("#"))
244
+ if (url.includes("#")) {
239
245
  window.location.reload();
246
+ }
240
247
  return;
241
248
  }
242
249
  const error = new URL(data.url).searchParams.get("error");
@@ -269,8 +276,9 @@ async function signOut(options) {
269
276
  if (options?.redirect ?? true) {
270
277
  const url = data.url ?? callbackUrl;
271
278
  window.location.href = url;
272
- if (url.includes("#"))
279
+ if (url.includes("#")) {
273
280
  window.location.reload();
281
+ }
274
282
  return;
275
283
  }
276
284
  await __AUTHJS._getSession({ event: "storage" });
@@ -285,8 +293,9 @@ function SessionProvider(props) {
285
293
  const hasInitialSession = props.session !== void 0;
286
294
  __AUTHJS._lastSync = hasInitialSession ? now() : 0;
287
295
  const [session, setSession] = React2.useState(() => {
288
- if (hasInitialSession)
296
+ if (hasInitialSession) {
289
297
  __AUTHJS._session = props.session;
298
+ }
290
299
  return props.session;
291
300
  });
292
301
  const [loading, setLoading] = React2.useState(!hasInitialSession);
@@ -340,8 +349,9 @@ function SessionProvider(props) {
340
349
  React2.useEffect(() => {
341
350
  const { refetchOnWindowFocus = true } = props;
342
351
  const visibilityHandler = () => {
343
- if (refetchOnWindowFocus && document.visibilityState === "visible")
352
+ if (refetchOnWindowFocus && document.visibilityState === "visible") {
344
353
  __AUTHJS._getSession({ event: "visibilitychange" });
354
+ }
345
355
  };
346
356
  document.addEventListener("visibilitychange", visibilityHandler, false);
347
357
  return () => document.removeEventListener("visibilitychange", visibilityHandler, false);
@@ -363,8 +373,9 @@ function SessionProvider(props) {
363
373
  data: session,
364
374
  status: loading ? "loading" : session ? "authenticated" : "unauthenticated",
365
375
  async update(data) {
366
- if (loading || !session)
376
+ if (loading || !session) {
367
377
  return;
378
+ }
368
379
  setLoading(true);
369
380
  const newSession = await fetchData(
370
381
  "session",
package/dist/react.mjs CHANGED
@@ -24,8 +24,9 @@ async function fetchData(path, config, logger2, req = {}) {
24
24
  }
25
25
  const res = await fetch(url, options);
26
26
  const data = await res.json();
27
- if (!res.ok)
27
+ if (!res.ok) {
28
28
  throw data;
29
+ }
29
30
  return data;
30
31
  } catch (error) {
31
32
  logger2.error(new ClientFetchError(error.message, error));
@@ -127,10 +128,11 @@ function useSession(options) {
127
128
  error: "SessionRequired",
128
129
  callbackUrl: window.location.href
129
130
  })}`;
130
- if (onUnauthenticated)
131
+ if (onUnauthenticated) {
131
132
  onUnauthenticated();
132
- else
133
+ } else {
133
134
  window.location.href = url;
135
+ }
134
136
  }
135
137
  }, [requiredAndNotLoading, onUnauthenticated]);
136
138
  if (requiredAndNotLoading) {
@@ -153,7 +155,11 @@ async function getSession(params) {
153
155
  return session;
154
156
  }
155
157
  async function getCsrfToken() {
156
- const response = await fetchData("csrf", authConfigManager.getConfig(), logger);
158
+ const response = await fetchData(
159
+ "csrf",
160
+ authConfigManager.getConfig(),
161
+ logger
162
+ );
157
163
  return response?.csrfToken ?? "";
158
164
  }
159
165
  async function getProviders() {
@@ -193,8 +199,9 @@ async function signIn(provider, options, authorizationParams) {
193
199
  if (redirect || !isSupportingReturn) {
194
200
  const url = data.url ?? callbackUrl;
195
201
  window.location.href = url;
196
- if (url.includes("#"))
202
+ if (url.includes("#")) {
197
203
  window.location.reload();
204
+ }
198
205
  return;
199
206
  }
200
207
  const error = new URL(data.url).searchParams.get("error");
@@ -227,8 +234,9 @@ async function signOut(options) {
227
234
  if (options?.redirect ?? true) {
228
235
  const url = data.url ?? callbackUrl;
229
236
  window.location.href = url;
230
- if (url.includes("#"))
237
+ if (url.includes("#")) {
231
238
  window.location.reload();
239
+ }
232
240
  return;
233
241
  }
234
242
  await __AUTHJS._getSession({ event: "storage" });
@@ -243,8 +251,9 @@ function SessionProvider(props) {
243
251
  const hasInitialSession = props.session !== void 0;
244
252
  __AUTHJS._lastSync = hasInitialSession ? now() : 0;
245
253
  const [session, setSession] = React2.useState(() => {
246
- if (hasInitialSession)
254
+ if (hasInitialSession) {
247
255
  __AUTHJS._session = props.session;
256
+ }
248
257
  return props.session;
249
258
  });
250
259
  const [loading, setLoading] = React2.useState(!hasInitialSession);
@@ -298,8 +307,9 @@ function SessionProvider(props) {
298
307
  React2.useEffect(() => {
299
308
  const { refetchOnWindowFocus = true } = props;
300
309
  const visibilityHandler = () => {
301
- if (refetchOnWindowFocus && document.visibilityState === "visible")
310
+ if (refetchOnWindowFocus && document.visibilityState === "visible") {
302
311
  __AUTHJS._getSession({ event: "visibilitychange" });
312
+ }
303
313
  };
304
314
  document.addEventListener("visibilitychange", visibilityHandler, false);
305
315
  return () => document.removeEventListener("visibilitychange", visibilityHandler, false);
@@ -321,8 +331,9 @@ function SessionProvider(props) {
321
331
  data: session,
322
332
  status: loading ? "loading" : session ? "authenticated" : "unauthenticated",
323
333
  async update(data) {
324
- if (loading || !session)
334
+ if (loading || !session) {
325
335
  return;
336
+ }
326
337
  setLoading(true);
327
338
  const newSession = await fetchData(
328
339
  "session",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/auth-js",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A third-party Auth js middleware for Hono",
5
5
  "main": "dist/index.js",
6
6
  "exports": {
@@ -53,11 +53,11 @@
53
53
  "homepage": "https://github.com/honojs/middleware",
54
54
  "peerDependencies": {
55
55
  "@auth/core": "0.*",
56
- "hono": "3.*",
56
+ "hono": ">=3.*",
57
57
  "react": ">=18"
58
58
  },
59
59
  "devDependencies": {
60
- "@auth/core": "^0.19.0",
60
+ "@auth/core": "^0.24.0",
61
61
  "@types/react": "^18",
62
62
  "hono": "^3.11.7",
63
63
  "jest": "^29.7.0",