@hono/auth-js 1.0.8 → 1.0.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.
package/dist/index.d.mts CHANGED
@@ -25,7 +25,7 @@ interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
25
25
  }
26
26
  type ConfigHandler = (c: Context) => AuthConfig;
27
27
  declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
28
- declare function reqWithEnvUrl(req: Request, authUrl?: string): Request;
28
+ declare function reqWithEnvUrl(req: Request, authUrl?: string): Promise<Request>;
29
29
  declare function getAuthUser(c: Context): Promise<AuthUser | null>;
30
30
  declare function verifyAuth(): MiddlewareHandler;
31
31
  declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
package/dist/index.d.ts CHANGED
@@ -25,7 +25,7 @@ interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
25
25
  }
26
26
  type ConfigHandler = (c: Context) => AuthConfig;
27
27
  declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
28
- declare function reqWithEnvUrl(req: Request, authUrl?: string): Request;
28
+ declare function reqWithEnvUrl(req: Request, authUrl?: string): Promise<Request>;
29
29
  declare function getAuthUser(c: Context): Promise<AuthUser | null>;
30
30
  declare function verifyAuth(): MiddlewareHandler;
31
31
  declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
package/dist/index.js CHANGED
@@ -37,35 +37,61 @@ function setEnvDefaults(env2, config) {
37
37
  config.basePath ||= "/api/auth";
38
38
  (0, import_core2.setEnvDefaults)(env2, config);
39
39
  }
40
- function reqWithEnvUrl(req, authUrl) {
40
+ async function cloneRequest(input, request) {
41
+ if ((0, import_adapter.getRuntimeKey)() === "bun") {
42
+ return new Request(input, {
43
+ method: request.method,
44
+ headers: new Headers(request.headers),
45
+ body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(),
46
+ // @ts-ignore: TS2353
47
+ referrer: "referrer" in request ? request.referrer : void 0,
48
+ // deno-lint-ignore no-explicit-any
49
+ referrerPolicy: request.referrerPolicy,
50
+ mode: request.mode,
51
+ credentials: request.credentials,
52
+ // @ts-ignore: TS2353
53
+ cache: request.cache,
54
+ redirect: request.redirect,
55
+ integrity: request.integrity,
56
+ keepalive: request.keepalive,
57
+ signal: request.signal
58
+ });
59
+ }
60
+ return new Request(input, request);
61
+ }
62
+ async function reqWithEnvUrl(req, authUrl) {
41
63
  if (authUrl) {
42
64
  const reqUrlObj = new URL(req.url);
43
65
  const authUrlObj = new URL(authUrl);
44
66
  const props = ["hostname", "protocol", "port", "password", "username"];
45
67
  props.forEach((prop) => reqUrlObj[prop] = authUrlObj[prop]);
46
- return new Request(reqUrlObj.href, req);
68
+ return cloneRequest(reqUrlObj.href, req);
47
69
  } else {
48
70
  const url = new URL(req.url);
49
71
  const proto = req.headers.get("x-forwarded-proto");
50
72
  const host = req.headers.get("x-forwarded-host") ?? req.headers.get("host");
51
73
  if (proto != null)
52
74
  url.protocol = proto.endsWith(":") ? proto : proto + ":";
53
- if (host) {
75
+ if (host != null) {
54
76
  url.host = host;
55
77
  const portMatch = host.match(/:(\d+)$/);
56
78
  if (portMatch)
57
79
  url.port = portMatch[1];
58
80
  else
59
81
  url.port = "";
82
+ req.headers.delete("x-forwarded-host");
83
+ req.headers.delete("Host");
84
+ req.headers.set("Host", host);
60
85
  }
61
- return new Request(url.href, req);
86
+ return cloneRequest(url.href, req);
62
87
  }
63
88
  }
64
89
  async function getAuthUser(c) {
65
90
  const config = c.get("authConfig");
66
- let ctxEnv = (0, import_adapter.env)(c);
91
+ const ctxEnv = (0, import_adapter.env)(c);
67
92
  setEnvDefaults(ctxEnv, config);
68
- const origin = new URL(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL).url).origin;
93
+ const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
94
+ const origin = new URL(authReq.url).origin;
69
95
  const request = new Request(`${origin}${config.basePath}/session`, {
70
96
  headers: { cookie: c.req.header("cookie") ?? "" }
71
97
  });
@@ -110,12 +136,13 @@ function initAuthConfig(cb) {
110
136
  function authHandler() {
111
137
  return async (c) => {
112
138
  const config = c.get("authConfig");
113
- let ctxEnv = (0, import_adapter.env)(c);
139
+ const ctxEnv = (0, import_adapter.env)(c);
114
140
  setEnvDefaults(ctxEnv, config);
115
141
  if (!config.secret || config.secret.length === 0) {
116
142
  throw new import_http_exception.HTTPException(500, { message: "Missing AUTH_SECRET" });
117
143
  }
118
- const res = await (0, import_core.Auth)(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config);
144
+ const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
145
+ const res = await (0, import_core.Auth)(authReq, config);
119
146
  return new Response(res.body, res);
120
147
  };
121
148
  }
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
2
  import { Auth } from "@auth/core";
3
- import { env } from "hono/adapter";
3
+ import { env, getRuntimeKey } from "hono/adapter";
4
4
  import { HTTPException } from "hono/http-exception";
5
5
  import { setEnvDefaults as coreSetEnvDefaults } from "@auth/core";
6
6
  function setEnvDefaults(env2, config) {
@@ -8,35 +8,61 @@ function setEnvDefaults(env2, config) {
8
8
  config.basePath ||= "/api/auth";
9
9
  coreSetEnvDefaults(env2, config);
10
10
  }
11
- function reqWithEnvUrl(req, authUrl) {
11
+ async function cloneRequest(input, request) {
12
+ if (getRuntimeKey() === "bun") {
13
+ return new Request(input, {
14
+ method: request.method,
15
+ headers: new Headers(request.headers),
16
+ body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(),
17
+ // @ts-ignore: TS2353
18
+ referrer: "referrer" in request ? request.referrer : void 0,
19
+ // deno-lint-ignore no-explicit-any
20
+ referrerPolicy: request.referrerPolicy,
21
+ mode: request.mode,
22
+ credentials: request.credentials,
23
+ // @ts-ignore: TS2353
24
+ cache: request.cache,
25
+ redirect: request.redirect,
26
+ integrity: request.integrity,
27
+ keepalive: request.keepalive,
28
+ signal: request.signal
29
+ });
30
+ }
31
+ return new Request(input, request);
32
+ }
33
+ async function reqWithEnvUrl(req, authUrl) {
12
34
  if (authUrl) {
13
35
  const reqUrlObj = new URL(req.url);
14
36
  const authUrlObj = new URL(authUrl);
15
37
  const props = ["hostname", "protocol", "port", "password", "username"];
16
38
  props.forEach((prop) => reqUrlObj[prop] = authUrlObj[prop]);
17
- return new Request(reqUrlObj.href, req);
39
+ return cloneRequest(reqUrlObj.href, req);
18
40
  } else {
19
41
  const url = new URL(req.url);
20
42
  const proto = req.headers.get("x-forwarded-proto");
21
43
  const host = req.headers.get("x-forwarded-host") ?? req.headers.get("host");
22
44
  if (proto != null)
23
45
  url.protocol = proto.endsWith(":") ? proto : proto + ":";
24
- if (host) {
46
+ if (host != null) {
25
47
  url.host = host;
26
48
  const portMatch = host.match(/:(\d+)$/);
27
49
  if (portMatch)
28
50
  url.port = portMatch[1];
29
51
  else
30
52
  url.port = "";
53
+ req.headers.delete("x-forwarded-host");
54
+ req.headers.delete("Host");
55
+ req.headers.set("Host", host);
31
56
  }
32
- return new Request(url.href, req);
57
+ return cloneRequest(url.href, req);
33
58
  }
34
59
  }
35
60
  async function getAuthUser(c) {
36
61
  const config = c.get("authConfig");
37
- let ctxEnv = env(c);
62
+ const ctxEnv = env(c);
38
63
  setEnvDefaults(ctxEnv, config);
39
- const origin = new URL(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL).url).origin;
64
+ const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
65
+ const origin = new URL(authReq.url).origin;
40
66
  const request = new Request(`${origin}${config.basePath}/session`, {
41
67
  headers: { cookie: c.req.header("cookie") ?? "" }
42
68
  });
@@ -81,12 +107,13 @@ function initAuthConfig(cb) {
81
107
  function authHandler() {
82
108
  return async (c) => {
83
109
  const config = c.get("authConfig");
84
- let ctxEnv = env(c);
110
+ const ctxEnv = env(c);
85
111
  setEnvDefaults(ctxEnv, config);
86
112
  if (!config.secret || config.secret.length === 0) {
87
113
  throw new HTTPException(500, { message: "Missing AUTH_SECRET" });
88
114
  }
89
- const res = await Auth(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config);
115
+ const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
116
+ const res = await Auth(authReq, config);
90
117
  return new Response(res.body, res);
91
118
  };
92
119
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/auth-js",
3
- "version": "1.0.8",
3
+ "version": "1.0.9",
4
4
  "description": "A third-party Auth js middleware for Hono",
5
5
  "main": "dist/index.js",
6
6
  "exports": {