diff --git a/packages/nextjs/src/app/actions.ts b/packages/nextjs/src/app/actions.ts new file mode 100644 index 00000000..7fe58141 --- /dev/null +++ b/packages/nextjs/src/app/actions.ts @@ -0,0 +1,46 @@ +"use server" + +import { OryConfig } from "../" +import { newOryProjectClient } from "../sdk" + +const client = newOryProjectClient() + +export async function getProjectConfig(): Promise { + const list = await client.listProjects().catch((err) => { + throw new Error( + "Unable to fetch the project list, please check your configuration and Ory Project API Key.", + err, + ) + }) + + if (list.length > 1) { + console.warn( + "Found more than one project for the configured Ory Project API Key. Using the first one.", + ) + } + + if (list.length === 0) { + throw new Error( + "Expected to find one project but found none. Please verify your configuration and check that your Ory Project API Key is valid for only one project.", + ) + } + + const project = await client + .getProject({ projectId: list[0].id }) + .catch((err) => { + throw new Error("Unable to fetch the project configuration.") + }) + + const config = project.services.identity?.config as any + return Promise.resolve({ + override: { + applicationName: project.name, + recovery_ui_path: config.selfservice.flows.recovery.ui_url, + registration_ui_path: config.selfservice.flows.registration.ui_url, + login_ui_path: config.selfservice.flows.login.ui_url, + settings_ui_path: config.selfservice.flows.settings.ui_url, + error_ui_path: config.selfservice.flows.error.ui_url, + verification_ui_path: config.selfservice.flows.verification.ui_url, + }, + }) +} diff --git a/packages/nextjs/src/app/login.ts b/packages/nextjs/src/app/login.ts index 7b25234c..eb518da1 100644 --- a/packages/nextjs/src/app/login.ts +++ b/packages/nextjs/src/app/login.ts @@ -3,14 +3,14 @@ import { FlowType, LoginFlow } from "@ory/client-fetch" import { QueryParams } from "../types" import { toFlowParams } from "./utils" import { getFlowFactory } from "./flow" -import { newFrontendClient } from "../sdk" +import { newOryFrontendClient } from "../sdk" const factory = getFlowFactory({ redirectToBrowserEndpoint, onRedirect, toFlowParams, flowType: FlowType.Login, - fetchFlow: newFrontendClient().getLoginFlowRaw, + fetchFlow: newOryFrontendClient().getLoginFlowRaw, }) /** diff --git a/packages/nextjs/src/app/registration.ts b/packages/nextjs/src/app/registration.ts index 8c5d0cac..d9e34a99 100644 --- a/packages/nextjs/src/app/registration.ts +++ b/packages/nextjs/src/app/registration.ts @@ -3,14 +3,14 @@ import { RegistrationFlow, FlowType } from "@ory/client-fetch" import { QueryParams } from "../types" import { toFlowParams } from "./utils" import { getFlowFactory } from "./flow" -import { newFrontendClient } from "../sdk" +import { newOryFrontendClient } from "../sdk" const factory = getFlowFactory({ redirectToBrowserEndpoint, onRedirect, toFlowParams, flowType: FlowType.Registration, - fetchFlow: newFrontendClient().getRegistrationFlowRaw, + fetchFlow: newOryFrontendClient().getRegistrationFlowRaw, }) /** diff --git a/packages/nextjs/src/hooks.ts b/packages/nextjs/src/hooks.ts index c9d59b82..27cc9a2a 100644 --- a/packages/nextjs/src/hooks.ts +++ b/packages/nextjs/src/hooks.ts @@ -1,9 +1,9 @@ -import { newFrontendClient } from "./sdk" +import { newOryFrontendClient } from "./sdk" import { Session } from "@ory/client-fetch" import { useEffect, useState } from "react" export function useSession() { - const client = newFrontendClient() + const client = newOryFrontendClient() const [session, setSession] = useState() useEffect(() => { diff --git a/packages/nextjs/src/index.ts b/packages/nextjs/src/index.ts index 0fcfd140..e85141b7 100644 --- a/packages/nextjs/src/index.ts +++ b/packages/nextjs/src/index.ts @@ -1,7 +1,7 @@ import { OryConfig } from "./types" import { useOryConfig } from "./config" import { useSession } from "./hooks" -import { newFrontendClient } from "./sdk" +import { newOryFrontendClient } from "./sdk" export type { OryConfig } -export { useOryConfig, newFrontendClient, useSession } +export { useOryConfig, newOryFrontendClient, useSession } diff --git a/packages/nextjs/src/pages/registration.ts b/packages/nextjs/src/pages/registration.ts index 83636d04..ffc72da3 100644 --- a/packages/nextjs/src/pages/registration.ts +++ b/packages/nextjs/src/pages/registration.ts @@ -1,13 +1,13 @@ import { FlowType, handleFlowError, RegistrationFlow } from "@ory/client-fetch" import { useEffect, useState } from "react" import { useRouter } from "next/router" -import { newFrontendClient } from "../sdk" +import { newOryFrontendClient } from "../sdk" import { onValidationError, toBrowserEndpointRedirect, toValue } from "../utils" import { GetServerSidePropsContext } from "next" import { useSearchParams } from "next/navigation" import { handleRestartFlow, toSearchParams, useOnRedirect } from "./utils" -const client = newFrontendClient() +const client = newOryFrontendClient() export interface RegistrationPageProps { flow: RegistrationFlow diff --git a/packages/nextjs/src/sdk.ts b/packages/nextjs/src/sdk.ts index 71e19d04..a5d51073 100644 --- a/packages/nextjs/src/sdk.ts +++ b/packages/nextjs/src/sdk.ts @@ -1,4 +1,4 @@ -import { Configuration, FrontendApi } from "@ory/client-fetch" +import { Configuration, FrontendApi, ProjectApi } from "@ory/client-fetch" const sdkUrl = process.env["ORY_SDK_URL"] || "" @@ -22,7 +22,17 @@ export function getSdkUrl() { return sdkUrl.replace(/\/$/, "") } -export function newFrontendClient() { +export function newOryClient(): { + frontend: FrontendApi + project: ProjectApi +} { + return { + frontend: newOryFrontendClient(), + project: newOryProjectClient(), + } +} + +export function newOryFrontendClient() { const config = new Configuration({ headers: { Accept: "application/json", @@ -31,3 +41,14 @@ export function newFrontendClient() { }) return new FrontendApi(config) } + +export function newOryProjectClient() { + const config = new Configuration({ + headers: { + Accept: "application/json", + }, + basePath: getSdkUrl(), + accessToken: process.env["ORY_PROJECT_API_TOKEN"], + }) + return new ProjectApi(config) +}