@hono/auth-js 1.0.7 → 1.0.8

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/README.md CHANGED
@@ -22,8 +22,9 @@ AUTH_URL=#optional
22
22
  ## How to Use
23
23
 
24
24
  ```ts
25
- import { Hono,Context } from 'hono'
26
- import { authHandler, initAuthConfig, verifyAuth, AuthConfig } from "@hono/auth-js"
25
+ import { Hono, Context } from 'hono'
26
+ import { authHandler, initAuthConfig, verifyAuth, type AuthConfig } from "@hono/auth-js"
27
+ import GitHub from "@auth/core/providers/github"
27
28
 
28
29
  const app = new Hono()
29
30
 
package/dist/index.d.mts CHANGED
@@ -24,10 +24,11 @@ type AuthUser = {
24
24
  interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
25
25
  }
26
26
  type ConfigHandler = (c: Context) => AuthConfig;
27
+ declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
27
28
  declare function reqWithEnvUrl(req: Request, authUrl?: string): Request;
28
29
  declare function getAuthUser(c: Context): Promise<AuthUser | null>;
29
30
  declare function verifyAuth(): MiddlewareHandler;
30
31
  declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
31
32
  declare function authHandler(): MiddlewareHandler;
32
33
 
33
- export { type AuthConfig, type AuthEnv, type AuthUser, type ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, verifyAuth };
34
+ export { type AuthConfig, type AuthEnv, type AuthUser, type ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
package/dist/index.d.ts CHANGED
@@ -24,10 +24,11 @@ type AuthUser = {
24
24
  interface AuthConfig extends Omit<AuthConfig$1, 'raw'> {
25
25
  }
26
26
  type ConfigHandler = (c: Context) => AuthConfig;
27
+ declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void;
27
28
  declare function reqWithEnvUrl(req: Request, authUrl?: string): Request;
28
29
  declare function getAuthUser(c: Context): Promise<AuthUser | null>;
29
30
  declare function verifyAuth(): MiddlewareHandler;
30
31
  declare function initAuthConfig(cb: ConfigHandler): MiddlewareHandler;
31
32
  declare function authHandler(): MiddlewareHandler;
32
33
 
33
- export { type AuthConfig, type AuthEnv, type AuthUser, type ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, verifyAuth };
34
+ export { type AuthConfig, type AuthEnv, type AuthUser, type ConfigHandler, authHandler, getAuthUser, initAuthConfig, reqWithEnvUrl, setEnvDefaults, verifyAuth };
package/dist/index.js CHANGED
@@ -24,12 +24,19 @@ __export(src_exports, {
24
24
  getAuthUser: () => getAuthUser,
25
25
  initAuthConfig: () => initAuthConfig,
26
26
  reqWithEnvUrl: () => reqWithEnvUrl,
27
+ setEnvDefaults: () => setEnvDefaults,
27
28
  verifyAuth: () => verifyAuth
28
29
  });
29
30
  module.exports = __toCommonJS(src_exports);
30
31
  var import_core = require("@auth/core");
31
32
  var import_adapter = require("hono/adapter");
32
33
  var import_http_exception = require("hono/http-exception");
34
+ var import_core2 = require("@auth/core");
35
+ function setEnvDefaults(env2, config) {
36
+ config.secret ??= env2.AUTH_SECRET;
37
+ config.basePath ||= "/api/auth";
38
+ (0, import_core2.setEnvDefaults)(env2, config);
39
+ }
33
40
  function reqWithEnvUrl(req, authUrl) {
34
41
  if (authUrl) {
35
42
  const reqUrlObj = new URL(req.url);
@@ -38,32 +45,27 @@ function reqWithEnvUrl(req, authUrl) {
38
45
  props.forEach((prop) => reqUrlObj[prop] = authUrlObj[prop]);
39
46
  return new Request(reqUrlObj.href, req);
40
47
  } else {
41
- return req;
42
- }
43
- }
44
- function setEnvDefaults(env2, config) {
45
- config.secret ??= env2.AUTH_SECRET;
46
- config.basePath ??= "/api/auth";
47
- config.trustHost = true;
48
- config.redirectProxyUrl ??= env2.AUTH_REDIRECT_PROXY_URL;
49
- config.providers = config.providers.map((p) => {
50
- const finalProvider = typeof p === "function" ? p({}) : p;
51
- if (finalProvider.type === "oauth" || finalProvider.type === "oidc") {
52
- const ID = finalProvider.id.toUpperCase();
53
- finalProvider.clientId ??= env2[`AUTH_${ID}_ID`];
54
- finalProvider.clientSecret ??= env2[`AUTH_${ID}_SECRET`];
55
- if (finalProvider.type === "oidc") {
56
- finalProvider.issuer ??= env2[`AUTH_${ID}_ISSUER`];
57
- }
48
+ const url = new URL(req.url);
49
+ const proto = req.headers.get("x-forwarded-proto");
50
+ const host = req.headers.get("x-forwarded-host") ?? req.headers.get("host");
51
+ if (proto != null)
52
+ url.protocol = proto.endsWith(":") ? proto : proto + ":";
53
+ if (host) {
54
+ url.host = host;
55
+ const portMatch = host.match(/:(\d+)$/);
56
+ if (portMatch)
57
+ url.port = portMatch[1];
58
+ else
59
+ url.port = "";
58
60
  }
59
- return finalProvider;
60
- });
61
+ return new Request(url.href, req);
62
+ }
61
63
  }
62
64
  async function getAuthUser(c) {
63
65
  const config = c.get("authConfig");
64
66
  let ctxEnv = (0, import_adapter.env)(c);
65
67
  setEnvDefaults(ctxEnv, config);
66
- const origin = ctxEnv.AUTH_URL ? new URL(ctxEnv.AUTH_URL).origin : new URL(c.req.url).origin;
68
+ const origin = new URL(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL).url).origin;
67
69
  const request = new Request(`${origin}${config.basePath}/session`, {
68
70
  headers: { cookie: c.req.header("cookie") ?? "" }
69
71
  });
@@ -110,7 +112,7 @@ function authHandler() {
110
112
  const config = c.get("authConfig");
111
113
  let ctxEnv = (0, import_adapter.env)(c);
112
114
  setEnvDefaults(ctxEnv, config);
113
- if (!config.secret) {
115
+ if (!config.secret || config.secret.length === 0) {
114
116
  throw new import_http_exception.HTTPException(500, { message: "Missing AUTH_SECRET" });
115
117
  }
116
118
  const res = await (0, import_core.Auth)(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config);
@@ -123,5 +125,6 @@ function authHandler() {
123
125
  getAuthUser,
124
126
  initAuthConfig,
125
127
  reqWithEnvUrl,
128
+ setEnvDefaults,
126
129
  verifyAuth
127
130
  });
package/dist/index.mjs CHANGED
@@ -2,6 +2,12 @@
2
2
  import { Auth } from "@auth/core";
3
3
  import { env } from "hono/adapter";
4
4
  import { HTTPException } from "hono/http-exception";
5
+ import { setEnvDefaults as coreSetEnvDefaults } from "@auth/core";
6
+ function setEnvDefaults(env2, config) {
7
+ config.secret ??= env2.AUTH_SECRET;
8
+ config.basePath ||= "/api/auth";
9
+ coreSetEnvDefaults(env2, config);
10
+ }
5
11
  function reqWithEnvUrl(req, authUrl) {
6
12
  if (authUrl) {
7
13
  const reqUrlObj = new URL(req.url);
@@ -10,32 +16,27 @@ function reqWithEnvUrl(req, authUrl) {
10
16
  props.forEach((prop) => reqUrlObj[prop] = authUrlObj[prop]);
11
17
  return new Request(reqUrlObj.href, req);
12
18
  } else {
13
- return req;
14
- }
15
- }
16
- function setEnvDefaults(env2, config) {
17
- config.secret ??= env2.AUTH_SECRET;
18
- config.basePath ??= "/api/auth";
19
- config.trustHost = true;
20
- config.redirectProxyUrl ??= env2.AUTH_REDIRECT_PROXY_URL;
21
- config.providers = config.providers.map((p) => {
22
- const finalProvider = typeof p === "function" ? p({}) : p;
23
- if (finalProvider.type === "oauth" || finalProvider.type === "oidc") {
24
- const ID = finalProvider.id.toUpperCase();
25
- finalProvider.clientId ??= env2[`AUTH_${ID}_ID`];
26
- finalProvider.clientSecret ??= env2[`AUTH_${ID}_SECRET`];
27
- if (finalProvider.type === "oidc") {
28
- finalProvider.issuer ??= env2[`AUTH_${ID}_ISSUER`];
29
- }
19
+ const url = new URL(req.url);
20
+ const proto = req.headers.get("x-forwarded-proto");
21
+ const host = req.headers.get("x-forwarded-host") ?? req.headers.get("host");
22
+ if (proto != null)
23
+ url.protocol = proto.endsWith(":") ? proto : proto + ":";
24
+ if (host) {
25
+ url.host = host;
26
+ const portMatch = host.match(/:(\d+)$/);
27
+ if (portMatch)
28
+ url.port = portMatch[1];
29
+ else
30
+ url.port = "";
30
31
  }
31
- return finalProvider;
32
- });
32
+ return new Request(url.href, req);
33
+ }
33
34
  }
34
35
  async function getAuthUser(c) {
35
36
  const config = c.get("authConfig");
36
37
  let ctxEnv = env(c);
37
38
  setEnvDefaults(ctxEnv, config);
38
- const origin = ctxEnv.AUTH_URL ? new URL(ctxEnv.AUTH_URL).origin : new URL(c.req.url).origin;
39
+ const origin = new URL(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL).url).origin;
39
40
  const request = new Request(`${origin}${config.basePath}/session`, {
40
41
  headers: { cookie: c.req.header("cookie") ?? "" }
41
42
  });
@@ -82,7 +83,7 @@ function authHandler() {
82
83
  const config = c.get("authConfig");
83
84
  let ctxEnv = env(c);
84
85
  setEnvDefaults(ctxEnv, config);
85
- if (!config.secret) {
86
+ if (!config.secret || config.secret.length === 0) {
86
87
  throw new HTTPException(500, { message: "Missing AUTH_SECRET" });
87
88
  }
88
89
  const res = await Auth(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config);
@@ -94,5 +95,6 @@ export {
94
95
  getAuthUser,
95
96
  initAuthConfig,
96
97
  reqWithEnvUrl,
98
+ setEnvDefaults,
97
99
  verifyAuth
98
100
  };
package/dist/react.d.mts CHANGED
@@ -6,19 +6,12 @@ interface AuthClientConfig {
6
6
  baseUrl: string;
7
7
  basePath: string;
8
8
  credentials?: RequestCredentials;
9
- /** Stores last session response */
10
9
  _session?: Session | null | undefined;
11
- /** Used for timestamp since last sycned (in seconds) */
12
10
  _lastSync: number;
13
- /**
14
- * Stores the `SessionProvider`'s session update method to be able to
15
- * trigger session updates from places like `signIn` or `signOut`
16
- */
17
11
  _getSession: (...args: any[]) => any;
18
12
  }
19
13
  interface UseSessionOptions<R extends boolean> {
20
14
  required: R;
21
- /** Defaults to `signIn` */
22
15
  onUnauthenticated?: () => void;
23
16
  }
24
17
  type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);
@@ -45,12 +38,7 @@ interface SignInResponse {
45
38
  ok: boolean;
46
39
  url: string | null;
47
40
  }
48
- /**
49
- * Match `inputType` of `new URLSearchParams(inputType)`
50
- * @internal
51
- */
52
41
  type SignInAuthorizationParams = string | string[][] | Record<string, string> | URLSearchParams;
53
- /** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option-1) */
54
42
  interface SignOutResponse {
55
43
  url: string;
56
44
  }
@@ -60,32 +48,13 @@ interface SignOutParams<R extends boolean = true> {
60
48
  /** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option-1 */
61
49
  redirect?: R;
62
50
  }
63
- /**
64
-
65
- * If you have session expiry times of 30 days (the default) or more, then you probably don't need to change any of the default options.
66
- *
67
- * However, if you need to customize the session behavior and/or are using short session expiry times, you can pass options to the provider to customize the behavior of the {@link useSession} hook.
68
- */
69
51
  interface SessionProviderProps {
70
52
  children: React.ReactNode;
71
53
  session?: Session | null;
72
54
  baseUrl?: string;
73
55
  basePath?: string;
74
- /**
75
- * A time interval (in seconds) after which the session will be re-fetched.
76
- * If set to `0` (default), the session is not polled.
77
- */
78
56
  refetchInterval?: number;
79
- /**
80
- * `SessionProvider` automatically refetches the session when the user switches between windows.
81
- * This option activates this behaviour if set to `true` (default).
82
- */
83
57
  refetchOnWindowFocus?: boolean;
84
- /**
85
- * Set to `false` to stop polling when the device has no internet access offline (determined by `navigator.onLine`)
86
- *
87
- * [`navigator.onLine` documentation](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine)
88
- */
89
58
  refetchWhenOffline?: false;
90
59
  }
91
60
 
@@ -97,7 +66,6 @@ declare class AuthConfigManager {
97
66
  getConfig(): AuthClientConfig;
98
67
  }
99
68
  declare const authConfigManager: AuthConfigManager;
100
- /** @todo Document */
101
69
  type UpdateSession = (data?: any) => Promise<Session | null>;
102
70
  type SessionContextValue<R extends boolean = false> = R extends true ? {
103
71
  update: UpdateSession;
package/dist/react.d.ts CHANGED
@@ -6,19 +6,12 @@ interface AuthClientConfig {
6
6
  baseUrl: string;
7
7
  basePath: string;
8
8
  credentials?: RequestCredentials;
9
- /** Stores last session response */
10
9
  _session?: Session | null | undefined;
11
- /** Used for timestamp since last sycned (in seconds) */
12
10
  _lastSync: number;
13
- /**
14
- * Stores the `SessionProvider`'s session update method to be able to
15
- * trigger session updates from places like `signIn` or `signOut`
16
- */
17
11
  _getSession: (...args: any[]) => any;
18
12
  }
19
13
  interface UseSessionOptions<R extends boolean> {
20
14
  required: R;
21
- /** Defaults to `signIn` */
22
15
  onUnauthenticated?: () => void;
23
16
  }
24
17
  type LiteralUnion<T extends U, U = string> = T | (U & Record<never, never>);
@@ -45,12 +38,7 @@ interface SignInResponse {
45
38
  ok: boolean;
46
39
  url: string | null;
47
40
  }
48
- /**
49
- * Match `inputType` of `new URLSearchParams(inputType)`
50
- * @internal
51
- */
52
41
  type SignInAuthorizationParams = string | string[][] | Record<string, string> | URLSearchParams;
53
- /** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option-1) */
54
42
  interface SignOutResponse {
55
43
  url: string;
56
44
  }
@@ -60,32 +48,13 @@ interface SignOutParams<R extends boolean = true> {
60
48
  /** [Documentation](https://next-auth.js.org/getting-started/client#using-the-redirect-false-option-1 */
61
49
  redirect?: R;
62
50
  }
63
- /**
64
-
65
- * If you have session expiry times of 30 days (the default) or more, then you probably don't need to change any of the default options.
66
- *
67
- * However, if you need to customize the session behavior and/or are using short session expiry times, you can pass options to the provider to customize the behavior of the {@link useSession} hook.
68
- */
69
51
  interface SessionProviderProps {
70
52
  children: React.ReactNode;
71
53
  session?: Session | null;
72
54
  baseUrl?: string;
73
55
  basePath?: string;
74
- /**
75
- * A time interval (in seconds) after which the session will be re-fetched.
76
- * If set to `0` (default), the session is not polled.
77
- */
78
56
  refetchInterval?: number;
79
- /**
80
- * `SessionProvider` automatically refetches the session when the user switches between windows.
81
- * This option activates this behaviour if set to `true` (default).
82
- */
83
57
  refetchOnWindowFocus?: boolean;
84
- /**
85
- * Set to `false` to stop polling when the device has no internet access offline (determined by `navigator.onLine`)
86
- *
87
- * [`navigator.onLine` documentation](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/onLine)
88
- */
89
58
  refetchWhenOffline?: false;
90
59
  }
91
60
 
@@ -97,7 +66,6 @@ declare class AuthConfigManager {
97
66
  getConfig(): AuthClientConfig;
98
67
  }
99
68
  declare const authConfigManager: AuthConfigManager;
100
- /** @todo Document */
101
69
  type UpdateSession = (data?: any) => Promise<Session | null>;
102
70
  type SessionContextValue<R extends boolean = false> = R extends true ? {
103
71
  update: UpdateSession;
package/dist/react.js CHANGED
@@ -79,9 +79,9 @@ function useOnline() {
79
79
  const [isOnline, setIsOnline] = React.useState(
80
80
  typeof navigator !== "undefined" ? navigator.onLine : false
81
81
  );
82
- const setOnline = () => setIsOnline(true);
83
- const setOffline = () => setIsOnline(false);
84
82
  React.useEffect(() => {
83
+ const setOnline = () => setIsOnline(true);
84
+ const setOffline = () => setIsOnline(false);
85
85
  window.addEventListener("online", setOnline);
86
86
  window.addEventListener("offline", setOffline);
87
87
  return () => {
@@ -95,16 +95,13 @@ function now() {
95
95
  return Math.floor(Date.now() / 1e3);
96
96
  }
97
97
  function parseUrl(url) {
98
- const defaultUrl = new URL("http://localhost:3000/api/auth");
99
- if (url && !url.startsWith("http")) {
100
- url = `https://${url}`;
101
- }
102
- const _url = new URL(url ?? defaultUrl);
103
- const path = (_url.pathname === "/" ? defaultUrl.pathname : _url.pathname).replace(/\/$/, "");
104
- const base = `${_url.origin}${path}`;
98
+ const defaultUrl = "http://localhost:3000/api/auth";
99
+ const parsedUrl = new URL(url?.startsWith("http") ? url : `https://${url}` || defaultUrl);
100
+ const path = parsedUrl.pathname === "/" ? "/api/auth" : parsedUrl.pathname.replace(/\/$/, "");
101
+ const base = `${parsedUrl.origin}${path}`;
105
102
  return {
106
- origin: _url.origin,
107
- host: _url.host,
103
+ origin: parsedUrl.origin,
104
+ host: parsedUrl.host,
108
105
  path,
109
106
  base,
110
107
  toString: () => base
package/dist/react.mjs CHANGED
@@ -37,9 +37,9 @@ function useOnline() {
37
37
  const [isOnline, setIsOnline] = React.useState(
38
38
  typeof navigator !== "undefined" ? navigator.onLine : false
39
39
  );
40
- const setOnline = () => setIsOnline(true);
41
- const setOffline = () => setIsOnline(false);
42
40
  React.useEffect(() => {
41
+ const setOnline = () => setIsOnline(true);
42
+ const setOffline = () => setIsOnline(false);
43
43
  window.addEventListener("online", setOnline);
44
44
  window.addEventListener("offline", setOffline);
45
45
  return () => {
@@ -53,16 +53,13 @@ function now() {
53
53
  return Math.floor(Date.now() / 1e3);
54
54
  }
55
55
  function parseUrl(url) {
56
- const defaultUrl = new URL("http://localhost:3000/api/auth");
57
- if (url && !url.startsWith("http")) {
58
- url = `https://${url}`;
59
- }
60
- const _url = new URL(url ?? defaultUrl);
61
- const path = (_url.pathname === "/" ? defaultUrl.pathname : _url.pathname).replace(/\/$/, "");
62
- const base = `${_url.origin}${path}`;
56
+ const defaultUrl = "http://localhost:3000/api/auth";
57
+ const parsedUrl = new URL(url?.startsWith("http") ? url : `https://${url}` || defaultUrl);
58
+ const path = parsedUrl.pathname === "/" ? "/api/auth" : parsedUrl.pathname.replace(/\/$/, "");
59
+ const base = `${parsedUrl.origin}${path}`;
63
60
  return {
64
- origin: _url.origin,
65
- host: _url.host,
61
+ origin: parsedUrl.origin,
62
+ host: parsedUrl.host,
66
63
  path,
67
64
  base,
68
65
  toString: () => base
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/auth-js",
3
- "version": "1.0.7",
3
+ "version": "1.0.8",
4
4
  "description": "A third-party Auth js middleware for Hono",
5
5
  "main": "dist/index.js",
6
6
  "exports": {