@hono/auth-js
Advanced tools
Comparing version 1.0.12 to 1.0.13
@@ -28,3 +28,3 @@ import { AuthConfig as AuthConfig$1 } from '@auth/core'; | ||
declare function setEnvDefaults(env: AuthEnv, config: AuthConfig): void; | ||
declare function reqWithEnvUrl(req: Request, authUrl?: string): Promise<Request>; | ||
declare function reqWithEnvUrl(req: Request, authUrl?: string): Request; | ||
declare function getAuthUser(c: Context): Promise<AuthUser | null>; | ||
@@ -31,0 +31,0 @@ declare function verifyAuth(): MiddlewareHandler; |
@@ -39,22 +39,3 @@ "use strict"; | ||
} | ||
async function cloneRequest(input, request, headers) { | ||
if ((0, import_adapter.getRuntimeKey)() === "bun") { | ||
return new Request(input, { | ||
method: request.method, | ||
headers: headers ?? new Headers(request.headers), | ||
body: request.method === "GET" || request.method === "HEAD" ? void 0 : await request.blob(), | ||
referrer: "referrer" in request ? request.referrer : void 0, | ||
referrerPolicy: request.referrerPolicy, | ||
mode: request.mode, | ||
credentials: request.credentials, | ||
cache: request.cache, | ||
redirect: request.redirect, | ||
integrity: request.integrity, | ||
keepalive: request.keepalive, | ||
signal: request.signal | ||
}); | ||
} | ||
return new Request(input, request); | ||
} | ||
async function reqWithEnvUrl(req, authUrl) { | ||
function reqWithEnvUrl(req, authUrl) { | ||
if (authUrl) { | ||
@@ -68,8 +49,8 @@ const reqUrlObj = new URL(req.url); | ||
} | ||
return cloneRequest(reqUrlObj.href, req); | ||
return new Request(reqUrlObj.href, req); | ||
} | ||
const url = new URL(req.url); | ||
const headers = new Headers(req.headers); | ||
const proto = headers.get("x-forwarded-proto"); | ||
const host = headers.get("x-forwarded-host") ?? headers.get("host"); | ||
const newReq = new Request(req); | ||
const url = new URL(newReq.url); | ||
const proto = newReq.headers.get("x-forwarded-proto"); | ||
const host = newReq.headers.get("x-forwarded-host") ?? newReq.headers.get("host"); | ||
if (proto != null) | ||
@@ -84,7 +65,7 @@ url.protocol = proto.endsWith(":") ? proto : `${proto}:`; | ||
url.port = ""; | ||
headers.delete("x-forwarded-host"); | ||
headers.delete("Host"); | ||
headers.set("Host", host); | ||
newReq.headers.delete("x-forwarded-host"); | ||
newReq.headers.delete("Host"); | ||
newReq.headers.set("Host", host); | ||
} | ||
return cloneRequest(url.href, req, headers); | ||
return new Request(url.href, newReq); | ||
} | ||
@@ -95,3 +76,3 @@ async function getAuthUser(c) { | ||
setEnvDefaults(ctxEnv, config); | ||
const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL); | ||
const authReq = reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL); | ||
const origin = new URL(authReq.url).origin; | ||
@@ -146,4 +127,3 @@ const request = new Request(`${origin}${config.basePath}/session`, { | ||
} | ||
const authReq = await reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL); | ||
const res = await (0, import_core.Auth)(authReq, config); | ||
const res = await (0, import_core.Auth)(reqWithEnvUrl(c.req.raw, ctxEnv.AUTH_URL), config); | ||
return new Response(res.body, res); | ||
@@ -150,0 +130,0 @@ }; |
@@ -72,2 +72,8 @@ import { BuiltInProviderType, ProviderType, RedirectableProviderType } from '@auth/core/providers'; | ||
}; | ||
type WindowProps = { | ||
url: string; | ||
title: string; | ||
width: number; | ||
height: number; | ||
}; | ||
@@ -102,3 +108,12 @@ declare class AuthConfigManager { | ||
declare function signOut<R extends boolean = true>(options?: SignOutParams<R>): Promise<R extends true ? undefined : SignOutResponse>; | ||
interface PopupLoginOptions extends Partial<Omit<WindowProps, 'url'>> { | ||
onSuccess?: () => void; | ||
callbackUrl?: string; | ||
} | ||
declare const useOauthPopupLogin: (provider: Parameters<typeof signIn>[0], options?: PopupLoginOptions) => { | ||
status: "loading" | "success" | "errored"; | ||
error?: string | undefined; | ||
popUpSignin: () => Promise<void>; | ||
}; | ||
export { SessionContext, SessionProvider, authConfigManager, getCsrfToken, getProviders, getSession, signIn, signOut, useSession }; | ||
export { SessionContext, SessionProvider, authConfigManager, getCsrfToken, getProviders, getSession, signIn, signOut, useOauthPopupLogin, useSession }; |
@@ -41,2 +41,3 @@ "use strict"; | ||
signOut: () => signOut, | ||
useOauthPopupLogin: () => useOauthPopupLogin, | ||
useSession: () => useSession | ||
@@ -388,2 +389,54 @@ }); | ||
} | ||
var createPopup = ({ url, title, height, width }) => { | ||
const left = window.screenX + (window.outerWidth - width) / 2; | ||
const top = window.screenY + (window.outerHeight - height) / 2.5; | ||
const externalPopup = window.open( | ||
url, | ||
title, | ||
`width=${width},height=${height},left=${left},top=${top}` | ||
); | ||
return externalPopup; | ||
}; | ||
var useOauthPopupLogin = (provider, options = {}) => { | ||
const { width = 500, height = 500, title = "Signin", onSuccess, callbackUrl = "/" } = options; | ||
const [externalWindow, setExternalWindow] = (0, import_react2.useState)(); | ||
const [state, setState] = (0, import_react2.useState)({ status: "loading" }); | ||
const popUpSignin = (0, import_react2.useCallback)(async () => { | ||
const res = await signIn(provider, { | ||
redirect: false, | ||
callbackUrl | ||
}); | ||
if (res?.error) { | ||
setState({ status: "errored", error: res.error }); | ||
return; | ||
} | ||
setExternalWindow( | ||
createPopup({ | ||
url: res?.url, | ||
title, | ||
width, | ||
height | ||
}) | ||
); | ||
}, []); | ||
(0, import_react2.useEffect)(() => { | ||
const handleMessage = (event) => { | ||
if (event.origin !== window.location.origin) | ||
return; | ||
if (event.data.status) { | ||
setState(event.data); | ||
if (event.data.status === "success") { | ||
onSuccess?.(); | ||
} | ||
externalWindow?.close(); | ||
} | ||
}; | ||
window.addEventListener("message", handleMessage); | ||
return () => { | ||
window.removeEventListener("message", handleMessage); | ||
externalWindow?.close(); | ||
}; | ||
}, [externalWindow]); | ||
return { popUpSignin, ...state }; | ||
}; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -399,3 +452,4 @@ 0 && (module.exports = { | ||
signOut, | ||
useOauthPopupLogin, | ||
useSession | ||
}); |
{ | ||
"name": "@hono/auth-js", | ||
"version": "1.0.12", | ||
"version": "1.0.13", | ||
"description": "A third-party Auth js middleware for Hono", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -115,7 +115,6 @@ # Auth.js middleware for Hono | ||
``` | ||
For more details on how to Popup Oauth Login see [example](https://github.com/divyam234/next-auth-hono-react) | ||
Working example repo https://github.com/divyam234/next-auth-hono-react | ||
## Author | ||
Divyam <https://github.com/divyam234> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
49853
1241
120