Skip to content

Commit

Permalink
chore: synchronize workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr authored and jonas-jonas committed Nov 8, 2024
1 parent 393b532 commit d2dea90
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 12 deletions.
46 changes: 46 additions & 0 deletions packages/nextjs/src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use server"

import { OryConfig } from "../"
import { newOryProjectClient } from "../sdk"

const client = newOryProjectClient()

export async function getProjectConfig(): Promise<OryConfig> {
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,
},
})
}
4 changes: 2 additions & 2 deletions packages/nextjs/src/app/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/app/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -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<Session | undefined>()

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 }
4 changes: 2 additions & 2 deletions packages/nextjs/src/pages/registration.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 23 additions & 2 deletions packages/nextjs/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -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"] || ""

Expand All @@ -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",
Expand All @@ -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)
}

0 comments on commit d2dea90

Please sign in to comment.