From 32fc3505da58da024d1453797b260a27573c5790 Mon Sep 17 00:00:00 2001 From: aeneasr <3372410+aeneasr@users.noreply.github.com> Date: Wed, 13 Nov 2024 16:20:05 +0100 Subject: [PATCH] chore: synchronize workspaces --- .../nextjs-app-router/app/auth/login/page.tsx | 1 - packages/nextjs/package.json | 5 -- packages/nextjs/src/app/client.ts | 1 + packages/nextjs/src/app/flow.ts | 43 --------------- packages/nextjs/src/app/index.ts | 6 +-- packages/nextjs/src/app/layout.tsx | 11 ++-- packages/nextjs/src/app/login.ts | 2 +- packages/nextjs/src/app/utils.ts | 24 +++------ packages/nextjs/src/hooks/index.ts | 3 -- packages/nextjs/src/middleware/middleware.ts | 1 + packages/nextjs/src/pages/client.ts | 2 +- packages/nextjs/src/pages/index.ts | 1 + packages/nextjs/src/pages/registration.ts | 12 +++-- packages/nextjs/src/pages/utils.ts | 53 ------------------- packages/nextjs/src/utils/cookie.test.ts | 3 ++ packages/nextjs/src/utils/cookie.ts | 6 --- packages/nextjs/src/utils/sdk.ts | 12 ----- packages/nextjs/src/utils/utils.ts | 12 ----- packages/nextjs/tsup.config.ts | 7 --- 19 files changed, 29 insertions(+), 176 deletions(-) delete mode 100644 packages/nextjs/src/app/flow.ts delete mode 100644 packages/nextjs/src/hooks/index.ts diff --git a/examples/nextjs-app-router/app/auth/login/page.tsx b/examples/nextjs-app-router/app/auth/login/page.tsx index a61397443..f8d6479a4 100644 --- a/examples/nextjs-app-router/app/auth/login/page.tsx +++ b/examples/nextjs-app-router/app/auth/login/page.tsx @@ -3,7 +3,6 @@ import { Login } from "@ory/elements-react/theme" import { getLoginFlow, OryPageParams } from "@ory/nextjs/app" -import "@ory/elements-react/theme/styles.css" import { enhanceConfig } from "@ory/nextjs" import config from "@/ory.config" diff --git a/packages/nextjs/package.json b/packages/nextjs/package.json index 14aa257f6..55fdcf16e 100644 --- a/packages/nextjs/package.json +++ b/packages/nextjs/package.json @@ -57,11 +57,6 @@ "types": "./dist/pages/index.d.ts", "import": "./dist/pages/index.mjs", "require": "./dist/pages/index.js" - }, - "./hooks": { - "types": "./dist/hooks/index.d.ts", - "import": "./dist/hooks/index.mjs", - "require": "./dist/hooks/index.js" } }, "typesVersions": { diff --git a/packages/nextjs/src/app/client.ts b/packages/nextjs/src/app/client.ts index 7d561cbe0..3fe6266f0 100644 --- a/packages/nextjs/src/app/client.ts +++ b/packages/nextjs/src/app/client.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { Configuration, FrontendApi } from "@ory/client-fetch" + import { orySdkUrl } from "../utils/sdk" export const serverSideFrontendClient = new FrontendApi( diff --git a/packages/nextjs/src/app/flow.ts b/packages/nextjs/src/app/flow.ts deleted file mode 100644 index db0d90074..000000000 --- a/packages/nextjs/src/app/flow.ts +++ /dev/null @@ -1,43 +0,0 @@ -// // Copyright © 2024 Ory Corp -// // SPDX-License-Identifier: Apache-2.0 -// import { FlowType, handleFlowError, ApiResponse } from "@ory/client-fetch" -// import { FlowParams, initOverrides, QueryParams } from "../types" -// import { onValidationError } from "../utils/utils" -// -// export function getFlowFactory(factory: { -// redirectToBrowserEndpoint: (params: QueryParams, flowType: FlowType) => void -// onRedirect: (url: string, external: boolean) => void -// toFlowParams: (params: QueryParams) => Promise -// flowType: FlowType -// fetchFlow: ( -// params: FlowParams, -// initOverrides: RequestInit, -// ) => Promise> -// }): (params: QueryParams) => Promise { -// return async (params: QueryParams) => { -// const onRestartFlow = () => -// factory.redirectToBrowserEndpoint(params, factory.flowType) -// -// if (!params["flow"]) { -// onRestartFlow() -// return -// } -// -// try { -// const resp = await factory.fetchFlow( -// await factory.toFlowParams(params), -// initOverrides, -// ) -// -// return await toValue(resp) -// } catch (error) { -// const errorHandler = handleFlowError({ -// onValidationError, -// onRestartFlow, -// onRedirect: factory.onRedirect, -// }) -// await errorHandler(error) -// return null -// } -// } -// } diff --git a/packages/nextjs/src/app/index.ts b/packages/nextjs/src/app/index.ts index b431295db..b233b6e63 100644 --- a/packages/nextjs/src/app/index.ts +++ b/packages/nextjs/src/app/index.ts @@ -1,8 +1,6 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 "use server" -export { getLoginFlow } from "./login" -export interface OryPageParams { - searchParams: URLSearchParams -} +export { getLoginFlow } from "./login" +export type { OryPageParams } from "./utils" diff --git a/packages/nextjs/src/app/layout.tsx b/packages/nextjs/src/app/layout.tsx index 09ea1720e..9285aff2b 100644 --- a/packages/nextjs/src/app/layout.tsx +++ b/packages/nextjs/src/app/layout.tsx @@ -1,14 +1,13 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 -import "@ory/elements-react/theme/styles.css" import { PropsWithChildren } from "react" +import "@ory/elements-react/theme/styles.css" + export default async function AuthLayout({ children }: PropsWithChildren) { return ( - <> -
- {children} -
- +
+ {children} +
) } diff --git a/packages/nextjs/src/app/login.ts b/packages/nextjs/src/app/login.ts index 22e81231c..5bc317c04 100644 --- a/packages/nextjs/src/app/login.ts +++ b/packages/nextjs/src/app/login.ts @@ -1,5 +1,6 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 +import { redirect, RedirectType } from "next/navigation" import { FlowType, handleFlowError, LoginFlow } from "@ory/client-fetch" import { getPublicUrl, onRedirect } from "./utils" @@ -9,7 +10,6 @@ import { guessPotentiallyProxiedOrySdkUrl } from "../utils/sdk" import { onValidationError } from "../utils/utils" import { rewriteJsonResponse } from "../utils/rewrite" import { serverSideFrontendClient } from "./client" -import { redirect, RedirectType } from "next/navigation" // const factory = getFlowFactory({ // redirectToBrowserEndpoint, diff --git a/packages/nextjs/src/app/utils.ts b/packages/nextjs/src/app/utils.ts index 69b4ff4f3..850181ea7 100644 --- a/packages/nextjs/src/app/utils.ts +++ b/packages/nextjs/src/app/utils.ts @@ -1,12 +1,11 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 -import { FlowType, OnRedirectHandler } from "@ory/client-fetch" import { headers } from "next/headers" -import { redirect, RedirectType } from "next/navigation" +import { redirect } from "next/navigation" +import { OnRedirectHandler } from "@ory/client-fetch" import { QueryParams } from "../types" import { toFlowParams as baseToFlowParams } from "../utils/utils" -import { guessPotentiallyProxiedOrySdkUrl } from "../utils/sdk" export async function getCookieHeader() { const h = await headers() @@ -21,24 +20,13 @@ export async function toFlowParams(params: QueryParams) { return baseToFlowParams(params, getCookieHeader) } -export function redirectToBrowserEndpoint( - params: QueryParams, - flowType: FlowType, -) { - // Take advantage of the fact, that Ory handles the flow creation for us and redirects the user to the default - // return to automatically if they're logged in already. - return redirect( - new URL( - "/self-service/" + flowType.toString() + "/browser?" + params.toString(), - guessPotentiallyProxiedOrySdkUrl(), - ).toString(), - RedirectType.replace, - ) -} - export async function getPublicUrl() { const h = await headers() const host = h.get("host") const protocol = h.get("x-forwarded-proto") || "http" return `${protocol}://${host}` } + +export interface OryPageParams { + searchParams: URLSearchParams +} diff --git a/packages/nextjs/src/hooks/index.ts b/packages/nextjs/src/hooks/index.ts deleted file mode 100644 index 5e2c58350..000000000 --- a/packages/nextjs/src/hooks/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -// Copyright © 2024 Ory Corp -// SPDX-License-Identifier: Apache-2.0 -"use client" diff --git a/packages/nextjs/src/middleware/middleware.ts b/packages/nextjs/src/middleware/middleware.ts index 446ed5e27..d11c97ca2 100644 --- a/packages/nextjs/src/middleware/middleware.ts +++ b/packages/nextjs/src/middleware/middleware.ts @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { NextResponse, type NextRequest } from "next/server" + import { rewriteUrls } from "../utils/rewrite" import { filterRequestHeaders, processSetCookieHeaders } from "../utils/utils" import { OryConfig } from "../types" diff --git a/packages/nextjs/src/pages/client.ts b/packages/nextjs/src/pages/client.ts index 7fb58252d..74f70667f 100644 --- a/packages/nextjs/src/pages/client.ts +++ b/packages/nextjs/src/pages/client.ts @@ -1,7 +1,7 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 - import { Configuration, FrontendApi } from "@ory/client-fetch" + import { guessPotentiallyProxiedOrySdkUrl } from "../utils/sdk" export const clientSideFrontendClient = new FrontendApi( diff --git a/packages/nextjs/src/pages/index.ts b/packages/nextjs/src/pages/index.ts index 1cb511cc7..2e705f95f 100644 --- a/packages/nextjs/src/pages/index.ts +++ b/packages/nextjs/src/pages/index.ts @@ -1,4 +1,5 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 "use client" + export { useRegistrationFlow } from "./registration" diff --git a/packages/nextjs/src/pages/registration.ts b/packages/nextjs/src/pages/registration.ts index 7b59d1219..fedede0c2 100644 --- a/packages/nextjs/src/pages/registration.ts +++ b/packages/nextjs/src/pages/registration.ts @@ -1,7 +1,9 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 -"use client" + import { useEffect, useState } from "react" +import { useRouter } from "next/router" +import { useSearchParams } from "next/navigation" import { FlowType, handleFlowError, @@ -9,10 +11,10 @@ import { RegistrationFlow, ApiResponse, } from "@ory/client-fetch" -import { useRouter } from "next/router" -import { useSearchParams } from "next/navigation" + import { clientSideFrontendClient } from "./client" import { rewriteJsonResponse } from "../utils/rewrite" +import { guessPotentiallyProxiedOrySdkUrl } from "../utils/sdk" export function toValue(res: ApiResponse): Promise { // Remove all undefined values from the response (array and object) using lodash: @@ -28,7 +30,9 @@ const toBrowserEndpointRedirect = ( params: URLSearchParams, flowType: FlowType, ) => - "http://localhost:3000" + + guessPotentiallyProxiedOrySdkUrl({ + knownProxiedUrl: window.location.origin, + }) + "/self-service/" + flowType.toString() + "/browser?" + diff --git a/packages/nextjs/src/pages/utils.ts b/packages/nextjs/src/pages/utils.ts index e75460064..b85af654c 100644 --- a/packages/nextjs/src/pages/utils.ts +++ b/packages/nextjs/src/pages/utils.ts @@ -1,55 +1,2 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 -"use client" -import { FlowType, OnRedirectHandler } from "@ory/client-fetch" -import { useRouter } from "next/router" -import type { ParsedUrlQuery } from "querystring" -import { guessPotentiallyProxiedOrySdkUrl } from "../utils/sdk" - -export const handleRestartFlow = - (searchParams: URLSearchParams, flowType: FlowType) => () => { - window.location.assign( - new URL( - "/self-service/" + - flowType.toString() + - "/browser?" + - searchParams.toString(), - guessPotentiallyProxiedOrySdkUrl(), - ).toString(), - ) - } - -export function useOnRedirect(): OnRedirectHandler { - const router = useRouter() - return (url: string, external: boolean) => { - if (external) { - window.location.assign(url) - } else { - router.push(url) - } - } -} - -export function toSearchParams(query: ParsedUrlQuery) { - // Convert ParsedUrlQuery to URLSearchParams - const searchParams = new URLSearchParams() - Object.entries(query).forEach(([key, value]) => { - if (!value) { - return - } - - // Handle array - if (Array.isArray(value)) { - value.forEach((v) => searchParams.append(key, v)) - return - } - searchParams.set(key, value) - }) - - return searchParams -} - -export function useSearchParams() { - const router = useRouter() - return toSearchParams(router.query) -} diff --git a/packages/nextjs/src/utils/cookie.test.ts b/packages/nextjs/src/utils/cookie.test.ts index 1362aa1a0..9e0e80209 100644 --- a/packages/nextjs/src/utils/cookie.test.ts +++ b/packages/nextjs/src/utils/cookie.test.ts @@ -1,3 +1,6 @@ +// Copyright © 2024 Ory Corp +// SPDX-License-Identifier: Apache-2.0 + import { guessCookieDomain } from "./cookie" describe("cookie guesser", () => { diff --git a/packages/nextjs/src/utils/cookie.ts b/packages/nextjs/src/utils/cookie.ts index f349deedc..201f0a9ac 100644 --- a/packages/nextjs/src/utils/cookie.ts +++ b/packages/nextjs/src/utils/cookie.ts @@ -4,12 +4,6 @@ import { OryConfig } from "../types" import { parse } from "psl" -function isIPv6(address: string): boolean { - const ipv6Pattern = - /^(?:[a-zA-Z][a-zA-Z\d+.-]*:\/\/)?(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$|^(?:[a-zA-Z][a-zA-Z\d+.-]*:\/\/)?::(?:[a-fA-F0-9]{1,4}:){0,6}[a-fA-F0-9]{1,4}$|^(?:[a-zA-Z][a-zA-Z\d+.-]*:\/\/)?(?:[a-fA-F0-9]{1,4}:){1,7}:$|^(?:[a-zA-F0-9]{1,4}:){1,6}:[a-fA-F0-9]{1,4}$|^(?:[a-fA-F0-9]{1,4}:){1,5}(?::[a-fA-F0-9]{1,4}){1,2}$|^(?:[a-fA-F0-9]{1,4}:){1,4}(?::[a-fA-F0-9]{1,4}){1,3}$|^(?:[a-fA-F0-9]{1,4}:){1,3}(?::[a-fA-F0-9]{1,4}){1,4}$|^(?:[a-fA-F0-9]{1,4}:){1,2}(?::[a-fA-F0-9]{1,4}){1,5}$|^[a-fA-F0-9]{1,4}:(?::[a-fA-F0-9]{1,4}){1,6}$|^:(?::[a-fA-F0-9]{1,4}){1,7}$|^::$/ - return ipv6Pattern.test(address) -} - export function guessCookieDomain(url: string | undefined, config: OryConfig) { if (!url || config.forceCookieDomain) { return config.forceCookieDomain diff --git a/packages/nextjs/src/utils/sdk.ts b/packages/nextjs/src/utils/sdk.ts index da0d99850..cca62630b 100644 --- a/packages/nextjs/src/utils/sdk.ts +++ b/packages/nextjs/src/utils/sdk.ts @@ -1,8 +1,6 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 -import { Configuration, FrontendApi } from "@ory/client-fetch" - export function orySdkUrl() { let baseUrl @@ -69,13 +67,3 @@ export function guessPotentiallyProxiedOrySdkUrl(options?: { return final } - -export function newOryFrontendClient(sdkUrl: string) { - const config = new Configuration({ - headers: { - Accept: "application/json", - }, - basePath: sdkUrl, - }) - return new FrontendApi(config) -} diff --git a/packages/nextjs/src/utils/utils.ts b/packages/nextjs/src/utils/utils.ts index 23f7178e4..3d66fc1b8 100644 --- a/packages/nextjs/src/utils/utils.ts +++ b/packages/nextjs/src/utils/utils.ts @@ -1,7 +1,6 @@ // Copyright © 2024 Ory Corp // SPDX-License-Identifier: Apache-2.0 -import { handleFlowError, OnRedirectHandler } from "@ory/client-fetch" import { parse, splitCookiesString } from "set-cookie-parser" import { serialize, SerializeOptions } from "cookie" @@ -24,17 +23,6 @@ export async function toFlowParams( } } -export const onError = - (onRestartFlow: () => void, onRedirect: OnRedirectHandler) => (err: any) => - new Promise((resolve) => { - handleFlowError({ - onValidationError: resolve, - // RestartFlow and Redirect both use redirects hence we don't need to resolve here. - onRestartFlow, - onRedirect, - })(err) - }) - export function processSetCookieHeaders( protocol: string, fetchResponse: Response, diff --git a/packages/nextjs/tsup.config.ts b/packages/nextjs/tsup.config.ts index b96d9eae3..e8e89e9af 100644 --- a/packages/nextjs/tsup.config.ts +++ b/packages/nextjs/tsup.config.ts @@ -55,11 +55,4 @@ export default defineConfig([ treeshake: true, external: reactExternal, }, - { - ...baseConfig, - entry: ["src/hooks/index.ts"], - outDir: "dist/hooks", - treeshake: true, - external: reactExternal, - }, ])