@hono/auth-js 1.0.12 → 1.0.13

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
@@ -112,8 +112,7 @@ const useSession = () => {
112
112
  return { session: data, status }
113
113
  }
114
114
  ```
115
-
116
- Working example repo https://github.com/divyam234/next-auth-hono-react
115
+ For more details on how to Popup Oauth Login see [example](https://github.com/divyam234/next-auth-hono-react)
117
116
 
118
117
  ## Author
119
118
 
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): Promise<Request>;
28
+ declare function reqWithEnvUrl(req: Request, authUrl?: string): 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): Promise<Request>;
28
+ declare function reqWithEnvUrl(req: Request, authUrl?: string): 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
@@ -36,26 +36,7 @@ function setEnvDefaults(env2, config) {
36
36
  config.secret ??= env2.AUTH_SECRET;
37
37
  (0, import_core2.setEnvDefaults)(env2, config);
38
38
  }
39
- async function cloneRequest(input, request, headers) {
40
- if ((0, import_adapter.getRuntimeKey)() === "bun") {
41
- return new Request(input, {
42
- method: request.method,
43
- headers: headers ?? new Headers(request.headers),
44
- body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(),
45
- referrer: "referrer" in request ? request.referrer : void 0,
46
- referrerPolicy: request.referrerPolicy,
47
- mode: request.mode,
48
- credentials: request.credentials,
49
- cache: request.cache,
50
- redirect: request.redirect,
51
- integrity: request.integrity,
52
- keepalive: request.keepalive,
53
- signal: request.signal
54
- });
55
- }
56
- return new Request(input, request);
57
- }
58
- async function reqWithEnvUrl(req, authUrl) {
39
+ function reqWithEnvUrl(req, authUrl) {
59
40
  if (authUrl) {
60
41
  const reqUrlObj = new URL(req.url);
61
42
  const authUrlObj = new URL(authUrl);
@@ -64,12 +45,12 @@ async function reqWithEnvUrl(req, authUrl) {
64
45
  if (authUrlObj[prop])
65
46
  reqUrlObj[prop] = authUrlObj[prop];
66
47
  }
67
- return cloneRequest(reqUrlObj.href, req);
48
+ return new Request(reqUrlObj.href, req);
68
49
  }
69
- const url = new URL(req.url);
70
- const headers = new Headers(req.headers);
71
- const proto = headers.get("x-forwarded-proto");
72
- const host = headers.get("x-forwarded-host") ?? headers.get("host");
50
+ const newReq = new Request(req);
51
+ const url = new URL(newReq.url);
52
+ const proto = newReq.headers.get("x-forwarded-proto");
53
+ const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host");
73
54
  if (proto != null)
74
55
  url.protocol = proto.endsWith(":") ? proto : `${proto}:`;
75
56
  if (host != null) {
@@ -79,17 +60,17 @@ async function reqWithEnvUrl(req, authUrl) {
79
60
  url.port = portMatch[1];
80
61
  else
81
62
  url.port = "";
82
- headers.delete("x-forwarded-host");
83
- headers.delete("Host");
84
- headers.set("Host", host);
63
+ newReq.headers.delete("x-forwarded-host");
64
+ newReq.headers.delete("Host");
65
+ newReq.headers.set("Host", host);
85
66
  }
86
- return cloneRequest(url.href, req, headers);
67
+ return new Request(url.href, newReq);
87
68
  }
88
69
  async function getAuthUser(c) {
89
70
  const config = c.get("authConfig");
90
71
  const ctxEnv = (0, import_adapter.env)(c);
91
72
  setEnvDefaults(ctxEnv, config);
92
- const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
73
+ const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
93
74
  const origin = new URL(authReq.url).origin;
94
75
  const request = new Request(`${origin}${config.basePath}/session`, {
95
76
  headers: { cookie: c.req.header("cookie") ?? "" }
@@ -139,8 +120,7 @@ function authHandler() {
139
120
  if (!config.secret || config.secret.length === 0) {
140
121
  throw new import_http_exception.HTTPException(500, { message: "Missing AUTH_SECRET" });
141
122
  }
142
- const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
143
- const res = await (0, import_core.Auth)(authReq, config);
123
+ const res = await (0, import_core.Auth)(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config);
144
124
  return new Response(res.body, res);
145
125
  };
146
126
  }
package/dist/index.mjs CHANGED
@@ -1,32 +1,13 @@
1
1
  // src/index.ts
2
2
  import { Auth } from "@auth/core";
3
- import { env, getRuntimeKey } from "hono/adapter";
3
+ import { env } 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) {
7
7
  config.secret ??= env2.AUTH_SECRET;
8
8
  coreSetEnvDefaults(env2, config);
9
9
  }
10
- async function cloneRequest(input, request, headers) {
11
- if (getRuntimeKey() === "bun") {
12
- return new Request(input, {
13
- method: request.method,
14
- headers: headers ?? new Headers(request.headers),
15
- body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(),
16
- referrer: "referrer" in request ? request.referrer : void 0,
17
- referrerPolicy: request.referrerPolicy,
18
- mode: request.mode,
19
- credentials: request.credentials,
20
- cache: request.cache,
21
- redirect: request.redirect,
22
- integrity: request.integrity,
23
- keepalive: request.keepalive,
24
- signal: request.signal
25
- });
26
- }
27
- return new Request(input, request);
28
- }
29
- async function reqWithEnvUrl(req, authUrl) {
10
+ function reqWithEnvUrl(req, authUrl) {
30
11
  if (authUrl) {
31
12
  const reqUrlObj = new URL(req.url);
32
13
  const authUrlObj = new URL(authUrl);
@@ -35,12 +16,12 @@ async function reqWithEnvUrl(req, authUrl) {
35
16
  if (authUrlObj[prop])
36
17
  reqUrlObj[prop] = authUrlObj[prop];
37
18
  }
38
- return cloneRequest(reqUrlObj.href, req);
19
+ return new Request(reqUrlObj.href, req);
39
20
  }
40
- const url = new URL(req.url);
41
- const headers = new Headers(req.headers);
42
- const proto = headers.get("x-forwarded-proto");
43
- const host = headers.get("x-forwarded-host") ?? headers.get("host");
21
+ const newReq = new Request(req);
22
+ const url = new URL(newReq.url);
23
+ const proto = newReq.headers.get("x-forwarded-proto");
24
+ const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host");
44
25
  if (proto != null)
45
26
  url.protocol = proto.endsWith(":") ? proto : `${proto}:`;
46
27
  if (host != null) {
@@ -50,17 +31,17 @@ async function reqWithEnvUrl(req, authUrl) {
50
31
  url.port = portMatch[1];
51
32
  else
52
33
  url.port = "";
53
- headers.delete("x-forwarded-host");
54
- headers.delete("Host");
55
- headers.set("Host", host);
34
+ newReq.headers.delete("x-forwarded-host");
35
+ newReq.headers.delete("Host");
36
+ newReq.headers.set("Host", host);
56
37
  }
57
- return cloneRequest(url.href, req, headers);
38
+ return new Request(url.href, newReq);
58
39
  }
59
40
  async function getAuthUser(c) {
60
41
  const config = c.get("authConfig");
61
42
  const ctxEnv = env(c);
62
43
  setEnvDefaults(ctxEnv, config);
63
- const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
44
+ const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
64
45
  const origin = new URL(authReq.url).origin;
65
46
  const request = new Request(`${origin}${config.basePath}/session`, {
66
47
  headers: { cookie: c.req.header("cookie") ?? "" }
@@ -110,8 +91,7 @@ function authHandler() {
110
91
  if (!config.secret || config.secret.length === 0) {
111
92
  throw new HTTPException(500, { message: "Missing AUTH_SECRET" });
112
93
  }
113
- const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL);
114
- const res = await Auth(authReq, config);
94
+ const res = await Auth(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config);
115
95
  return new Response(res.body, res);
116
96
  };
117
97
  }
package/dist/react.d.mts CHANGED
@@ -69,6 +69,12 @@ type SessionContextValue<R extends boolean = false> = R extends true ? {
69
69
  data: null;
70
70
  status: 'unauthenticated' | 'loading';
71
71
  };
72
+ type WindowProps = {
73
+ url: string;
74
+ title: string;
75
+ width: number;
76
+ height: number;
77
+ };
72
78
 
73
79
  declare class AuthConfigManager {
74
80
  private static instance;
@@ -98,5 +104,14 @@ type ProvidersType = Record<LiteralUnion<BuiltInProviderType>, ClientSafeProvide
98
104
  declare function getProviders(): Promise<ProvidersType | null>;
99
105
  declare function signIn<P extends RedirectableProviderType | undefined = undefined>(provider?: LiteralUnion<P extends RedirectableProviderType ? P | BuiltInProviderType : BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorizationParams): Promise<P extends RedirectableProviderType ? SignInResponse | undefined : undefined>;
100
106
  declare function signOut<R extends boolean = true>(options?: SignOutParams<R>): Promise<R extends true ? undefined : SignOutResponse>;
107
+ interface PopupLoginOptions extends Partial<Omit<WindowProps, 'url'>> {
108
+ onSuccess?: () => void;
109
+ callbackUrl?: string;
110
+ }
111
+ declare const useOauthPopupLogin: (provider: Parameters<typeof signIn>[0], options?: PopupLoginOptions) => {
112
+ status: "loading" | "success" | "errored";
113
+ error?: string | undefined;
114
+ popUpSignin: () => Promise<void>;
115
+ };
101
116
 
102
- export { SessionContext, SessionProvider, authConfigManager, getCsrfToken, getProviders, getSession, signIn, signOut, useSession };
117
+ export { SessionContext, SessionProvider, authConfigManager, getCsrfToken, getProviders, getSession, signIn, signOut, useOauthPopupLogin, useSession };
package/dist/react.d.ts CHANGED
@@ -69,6 +69,12 @@ type SessionContextValue<R extends boolean = false> = R extends true ? {
69
69
  data: null;
70
70
  status: 'unauthenticated' | 'loading';
71
71
  };
72
+ type WindowProps = {
73
+ url: string;
74
+ title: string;
75
+ width: number;
76
+ height: number;
77
+ };
72
78
 
73
79
  declare class AuthConfigManager {
74
80
  private static instance;
@@ -98,5 +104,14 @@ type ProvidersType = Record<LiteralUnion<BuiltInProviderType>, ClientSafeProvide
98
104
  declare function getProviders(): Promise<ProvidersType | null>;
99
105
  declare function signIn<P extends RedirectableProviderType | undefined = undefined>(provider?: LiteralUnion<P extends RedirectableProviderType ? P | BuiltInProviderType : BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorizationParams): Promise<P extends RedirectableProviderType ? SignInResponse | undefined : undefined>;
100
106
  declare function signOut<R extends boolean = true>(options?: SignOutParams<R>): Promise<R extends true ? undefined : SignOutResponse>;
107
+ interface PopupLoginOptions extends Partial<Omit<WindowProps, 'url'>> {
108
+ onSuccess?: () => void;
109
+ callbackUrl?: string;
110
+ }
111
+ declare const useOauthPopupLogin: (provider: Parameters<typeof signIn>[0], options?: PopupLoginOptions) => {
112
+ status: "loading" | "success" | "errored";
113
+ error?: string | undefined;
114
+ popUpSignin: () => Promise<void>;
115
+ };
101
116
 
102
- export { SessionContext, SessionProvider, authConfigManager, getCsrfToken, getProviders, getSession, signIn, signOut, useSession };
117
+ export { SessionContext, SessionProvider, authConfigManager, getCsrfToken, getProviders, getSession, signIn, signOut, useOauthPopupLogin, useSession };
package/dist/react.js CHANGED
@@ -38,6 +38,7 @@ __export(react_exports, {
38
38
  getSession: () => getSession,
39
39
  signIn: () => signIn,
40
40
  signOut: () => signOut,
41
+ useOauthPopupLogin: () => useOauthPopupLogin,
41
42
  useSession: () => useSession
42
43
  });
43
44
  module.exports = __toCommonJS(react_exports);
@@ -384,6 +385,58 @@ async function signOut(options) {
384
385
  await config.fetchSession?.({ event: "storage" });
385
386
  return data;
386
387
  }
388
+ var createPopup = ({ url, title, height, width }) => {
389
+ const left = window.screenX + (window.outerWidth - width) / 2;
390
+ const top = window.screenY + (window.outerHeight - height) / 2.5;
391
+ const externalPopup = window.open(
392
+ url,
393
+ title,
394
+ `width=${width},height=${height},left=${left},top=${top}`
395
+ );
396
+ return externalPopup;
397
+ };
398
+ var useOauthPopupLogin = (provider, options = {}) => {
399
+ const { width = 500, height = 500, title = "Signin", onSuccess, callbackUrl = "/" } = options;
400
+ const [externalWindow, setExternalWindow] = (0, import_react2.useState)();
401
+ const [state, setState] = (0, import_react2.useState)({ status: "loading" });
402
+ const popUpSignin = (0, import_react2.useCallback)(async () => {
403
+ const res = await signIn(provider, {
404
+ redirect: false,
405
+ callbackUrl
406
+ });
407
+ if (res?.error) {
408
+ setState({ status: "errored", error: res.error });
409
+ return;
410
+ }
411
+ setExternalWindow(
412
+ createPopup({
413
+ url: res?.url,
414
+ title,
415
+ width,
416
+ height
417
+ })
418
+ );
419
+ }, []);
420
+ (0, import_react2.useEffect)(() => {
421
+ const handleMessage = (event) => {
422
+ if (event.origin !== window.location.origin)
423
+ return;
424
+ if (event.data.status) {
425
+ setState(event.data);
426
+ if (event.data.status === "success") {
427
+ onSuccess?.();
428
+ }
429
+ externalWindow?.close();
430
+ }
431
+ };
432
+ window.addEventListener("message", handleMessage);
433
+ return () => {
434
+ window.removeEventListener("message", handleMessage);
435
+ externalWindow?.close();
436
+ };
437
+ }, [externalWindow]);
438
+ return { popUpSignin, ...state };
439
+ };
387
440
  // Annotate the CommonJS export names for ESM import in node:
388
441
  0 && (module.exports = {
389
442
  SessionContext,
@@ -394,5 +447,6 @@ async function signOut(options) {
394
447
  getSession,
395
448
  signIn,
396
449
  signOut,
450
+ useOauthPopupLogin,
397
451
  useSession
398
452
  });
package/dist/react.mjs CHANGED
@@ -68,7 +68,7 @@ function parseUrl(url) {
68
68
  }
69
69
 
70
70
  // src/react.tsx
71
- import { useContext, useEffect as useEffect2, useMemo } from "react";
71
+ import { useCallback, useContext, useEffect as useEffect2, useMemo, useState as useState3 } from "react";
72
72
  var logger = {
73
73
  debug: console.debug,
74
74
  error: console.error,
@@ -342,6 +342,58 @@ async function signOut(options) {
342
342
  await config.fetchSession?.({ event: "storage" });
343
343
  return data;
344
344
  }
345
+ var createPopup = ({ url, title, height, width }) => {
346
+ const left = window.screenX + (window.outerWidth - width) / 2;
347
+ const top = window.screenY + (window.outerHeight - height) / 2.5;
348
+ const externalPopup = window.open(
349
+ url,
350
+ title,
351
+ `width=${width},height=${height},left=${left},top=${top}`
352
+ );
353
+ return externalPopup;
354
+ };
355
+ var useOauthPopupLogin = (provider, options = {}) => {
356
+ const { width = 500, height = 500, title = "Signin", onSuccess, callbackUrl = "/" } = options;
357
+ const [externalWindow, setExternalWindow] = useState3();
358
+ const [state, setState] = useState3({ status: "loading" });
359
+ const popUpSignin = useCallback(async () => {
360
+ const res = await signIn(provider, {
361
+ redirect: false,
362
+ callbackUrl
363
+ });
364
+ if (res?.error) {
365
+ setState({ status: "errored", error: res.error });
366
+ return;
367
+ }
368
+ setExternalWindow(
369
+ createPopup({
370
+ url: res?.url,
371
+ title,
372
+ width,
373
+ height
374
+ })
375
+ );
376
+ }, []);
377
+ useEffect2(() => {
378
+ const handleMessage = (event) => {
379
+ if (event.origin !== window.location.origin)
380
+ return;
381
+ if (event.data.status) {
382
+ setState(event.data);
383
+ if (event.data.status === "success") {
384
+ onSuccess?.();
385
+ }
386
+ externalWindow?.close();
387
+ }
388
+ };
389
+ window.addEventListener("message", handleMessage);
390
+ return () => {
391
+ window.removeEventListener("message", handleMessage);
392
+ externalWindow?.close();
393
+ };
394
+ }, [externalWindow]);
395
+ return { popUpSignin, ...state };
396
+ };
345
397
  export {
346
398
  SessionContext,
347
399
  SessionProvider,
@@ -351,5 +403,6 @@ export {
351
403
  getSession,
352
404
  signIn,
353
405
  signOut,
406
+ useOauthPopupLogin,
354
407
  useSession
355
408
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hono/auth-js",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "description": "A third-party Auth js middleware for Hono",
5
5
  "main": "dist/index.js",
6
6
  "exports": {