Skip to content

Commit

Permalink
chore: synchronize workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Nov 13, 2024
1 parent db3416a commit 32fc350
Show file tree
Hide file tree
Showing 19 changed files with 29 additions and 176 deletions.
1 change: 0 additions & 1 deletion examples/nextjs-app-router/app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 0 additions & 5 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/app/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
43 changes: 0 additions & 43 deletions packages/nextjs/src/app/flow.ts

This file was deleted.

6 changes: 2 additions & 4 deletions packages/nextjs/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 5 additions & 6 deletions packages/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<>
<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen">
{children}
</main>
</>
<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen">
{children}
</main>
)
}
2 changes: 1 addition & 1 deletion packages/nextjs/src/app/login.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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,
Expand Down
24 changes: 6 additions & 18 deletions packages/nextjs/src/app/utils.ts
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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
}
3 changes: 0 additions & 3 deletions packages/nextjs/src/hooks/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/nextjs/src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/src/pages/client.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
1 change: 1 addition & 0 deletions packages/nextjs/src/pages/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
"use client"

export { useRegistrationFlow } from "./registration"
12 changes: 8 additions & 4 deletions packages/nextjs/src/pages/registration.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// 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,
OnRedirectHandler,
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<T extends object>(res: ApiResponse<T>): Promise<T> {
// Remove all undefined values from the response (array and object) using lodash:
Expand All @@ -28,7 +30,9 @@ const toBrowserEndpointRedirect = (
params: URLSearchParams,
flowType: FlowType,
) =>
"http://localhost:3000" +
guessPotentiallyProxiedOrySdkUrl({
knownProxiedUrl: window.location.origin,
}) +
"/self-service/" +
flowType.toString() +
"/browser?" +
Expand Down
53 changes: 0 additions & 53 deletions packages/nextjs/src/pages/utils.ts
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions packages/nextjs/src/utils/cookie.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

import { guessCookieDomain } from "./cookie"

describe("cookie guesser", () => {
Expand Down
6 changes: 0 additions & 6 deletions packages/nextjs/src/utils/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions packages/nextjs/src/utils/sdk.ts
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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)
}
12 changes: 0 additions & 12 deletions packages/nextjs/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -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"

Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions packages/nextjs/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,4 @@ export default defineConfig([
treeshake: true,
external: reactExternal,
},
{
...baseConfig,
entry: ["src/hooks/index.ts"],
outDir: "dist/hooks",
treeshake: true,
external: reactExternal,
},
])

0 comments on commit 32fc350

Please sign in to comment.