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 11, 2024
1 parent 7d17f81 commit b32f72f
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 78 deletions.
2 changes: 1 addition & 1 deletion examples/nextjs-app-router/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { createOryMiddleware } from "@ory/nextjs/middleware"
import oryConfig from "./ory.config"
import oryConfig from "@/ory.config"

// This function can be marked `async` if using `await` inside
export const middleware = createOryMiddleware(oryConfig)
Expand Down
2 changes: 2 additions & 0 deletions examples/nextjs-pages-router/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
NEXT_PUBLIC_ORY_SDK_URL=https://amazing-goldberg-vrybkrc0c1.projects.oryapis.com
ORY_SDK_URL=https://amazing-goldberg-vrybkrc0c1.projects.oryapis.com
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0

import { Registration } from "@ory/elements-react/theme"
import {
useRegistrationFlow,
getRegistrationServerSideProps,
} from "@ory/nextjs/pages"

import { useOryConfig } from "@ory/nextjs"
import config from "nextjs-app-router/ory.config"

// This gets called on every request
export const getServerSideProps = getRegistrationServerSideProps
import config from "@/ory.config"

export default async function RegistrationPage() {
const flow = await useRegistrationFlow()
export default function LoginPage() {
const flow = useRegistrationFlow()

if (!flow) {
return null
Expand Down
23 changes: 18 additions & 5 deletions examples/nextjs-pages-router/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
Expand All @@ -13,9 +17,18 @@
"jsx": "preserve",
"incremental": true,
"paths": {
"@/*": ["./*"]
}
"@/*": [
"./*"
]
},
"target": "ES2017"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/nextjs/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
"options": {
"cwd": "packages/nextjs"
}
},
"dev": {
"command": "tsup --watch --dts",
"options": {
"cwd": "packages/nextjs"
}
}
}
}
12 changes: 1 addition & 11 deletions packages/nextjs/src/middleware/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ import { rewriteUrls } from "../rewrite"
import { filterRequestHeaders, processSetCookieHeaders } from "../utils"
import { OryConfig } from "../types"
import { defaultOmitHeaders } from "../headers"

function getProjectSdkUrl() {
let baseUrl = ""

if (process.env["ORY_SDK_URL"]) {
baseUrl = process.env["ORY_SDK_URL"]
}

return baseUrl.replace(/\/$/, "")
}
import {getProjectSdkUrl} from "../sdk";

function getProjectApiKey() {
let baseUrl = ""
Expand Down Expand Up @@ -104,7 +95,6 @@ async function proxyRequest(request: NextRequest, options: OryConfig) {
selfUrl,
options,
)
console.log({originalLocation, location})

if (!location.startsWith("http")) {
// console.debug('rewriting location', selfUrl, location, new URL(location, selfUrl).toString())
Expand Down
3 changes: 2 additions & 1 deletion packages/nextjs/src/pages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

export {
useRegistrationFlow,
getRegistrationServerSideProps,
} from "./registration"

export type {RegistrationPageProps} from "./registration"
112 changes: 65 additions & 47 deletions packages/nextjs/src/pages/registration.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
// Copyright © 2024 Ory Corp
// SPDX-License-Identifier: Apache-2.0
"use client"
import { FlowType, handleFlowError, RegistrationFlow } from "@ory/client-fetch"

import { useEffect, useState } from "react"
import {
FlowType,
handleFlowError,
RegistrationFlow,
} from "@ory/client-fetch"
import { useRouter } from "next/router"
import { newOryFrontendClient } from "../sdk"
import { onValidationError, toBrowserEndpointRedirect, toValue } from "../utils"
import { GetServerSidePropsContext } from "next"
import { onValidationError } from "../utils"
import { useSearchParams } from "next/navigation"
import { handleRestartFlow, toSearchParams, useOnRedirect } from "./utils"
import { handleRestartFlow, useOnRedirect } from "./utils"

const client = newOryFrontendClient()

export interface RegistrationPageProps {
flow: RegistrationFlow
}

export async function useRegistrationFlow(): Promise<RegistrationFlow | null | void> {
export function useRegistrationFlow(): RegistrationFlow | null | void {
const [flow, setFlow] = useState<RegistrationFlow>()
const router = useRouter()
const searchParams = useSearchParams()
Expand Down Expand Up @@ -52,40 +51,59 @@ export async function useRegistrationFlow(): Promise<RegistrationFlow | null | v
return flow
}

// This gets called on every request
export async function getRegistrationServerSideProps(
context: GetServerSidePropsContext,
) {
// Otherwise we initialize it
const query = toSearchParams(context.query)
return await client
.createBrowserRegistrationFlowRaw({
returnTo: query.get("return_to") ?? undefined,
loginChallenge: query.get("login_challenge") ?? undefined,
afterVerificationReturnTo:
query.get("after_verification_return_to") ?? undefined,
organization: query.get("organization") ?? undefined,
})
.then(toValue)
.then((v) => ({ props: { flow: v } }))
.catch(
handleFlowError({
onValidationError,
onRestartFlow: () => ({
redirect: {
destination: toBrowserEndpointRedirect(
query,
FlowType.Registration,
),
permanent: false,
},
}),
onRedirect: (url) => ({
redirect: {
destination: url,
permanent: false,
},
}),
}),
)
}
// // This gets called on every request
// export async function getRegistrationServerSideProps(
// context: GetServerSidePropsContext,
// ) {
// // Otherwise we initialize it
// const query = toSearchParams(context.query)
// return await client
// .createBrowserRegistrationFlowRaw({
// returnTo: query.get("return_to") ?? undefined,
// loginChallenge: query.get("login_challenge") ?? undefined,
// afterVerificationReturnTo:
// query.get("after_verification_return_to") ?? undefined,
// organization: query.get("organization") ?? undefined,
// })
// .then(toValue)
// .then((v) => ({
// props: { flow: replaceUndefinedWithNull(RegistrationFlowToJSON(v)) },
// }))
// .catch(
// handleFlowError({
// onValidationError,
// onRestartFlow: () => ({
// redirect: {
// destination: toBrowserEndpointRedirect(
// query,
// FlowType.Registration,
// ),
// permanent: false,
// },
// }),
// onRedirect: (url) => ({
// redirect: {
// destination: url,
// permanent: false,
// },
// }),
// }),
// )
// }
//
// function replaceUndefinedWithNull(obj: unknown): unknown {
// if (Array.isArray(obj)) {
// return obj.map(replaceUndefinedWithNull)
// } else if (obj !== null && typeof obj === "object") {
// Object.keys(obj).forEach((key) => {
// const value = (obj as Record<string, unknown>)[key]
// if (value === undefined) {
// delete (obj as Record<string, unknown>)[key]
// } else {
// (obj as Record<string, unknown>)[key] = replaceUndefinedWithNull(value)
// }
// })
// return obj
// }
// return obj
// }
2 changes: 1 addition & 1 deletion packages/nextjs/src/rewrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function rewriteUrls(
["/ui/verification", config.override?.verificationUiPath],
["/ui/settings", config.override?.settingsUiPath],
].entries()) {
console.log(matchPath, replaceWith)

const match = joinUrlPaths(matchBaseUrl, matchPath || "")
if (replaceWith && source.startsWith(match)) {
source = source.replaceAll(
Expand Down
22 changes: 20 additions & 2 deletions packages/nextjs/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@

import { Configuration, FrontendApi, ProjectApi } from "@ory/client-fetch"

const sdkUrl = process.env["ORY_SDK_URL"] || ""
export function getProjectSdkUrl() {
let baseUrl = ""

if (process.env["NEXT_PUBLIC_ORY_SDK_URL"]) {
baseUrl = process.env["NEXT_PUBLIC_ORY_SDK_URL"]
}

if (process.env["ORY_SDK_URL"]) {
baseUrl = process.env["ORY_SDK_URL"]
}

if (!baseUrl) {
throw new Error(
"You need to set environment variable ORY_SDK_URL to your Ory Network SDK URL.",
)
}

return baseUrl.replace(/\/$/, "")
}

function isProduction() {
return (
Expand All @@ -22,7 +40,7 @@ export function getSdkUrl() {
return `https://${process.env["VERCEL_URL"]}`.replace(/\/$/, "")
}

return sdkUrl.replace(/\/$/, "")
return getProjectSdkUrl().replace(/\/$/, "")
}

export function newOryClient(): {
Expand Down

0 comments on commit b32f72f

Please sign in to comment.