{"version":3,"sources":["../../src/utils/sdk.ts","../../src/pages/client.ts","../../src/pages/utils.ts","../../src/utils/rewrite.ts","../../src/utils/utils.ts","../../src/pages/flow.ts","../../src/pages/registration.ts","../../src/pages/verification.ts","../../src/pages/recovery.ts","../../src/pages/login.ts","../../src/pages/settings.ts","../../src/pages/logout.ts"],"names":["useRouter","flow","FlowType","useState","useEffect"],"mappings":";;;;;;;;;AAMA,SAAS,OAAO,IAAkC,EAAA;AAChD,EAAO,OAAA,OAAA,CAAQ,IAAI,CAAe,YAAA,EAAA,IAAI,EAAE,CAAK,IAAA,OAAA,CAAQ,IAAI,IAAI,CAAA;AAC/D;AAEO,SAAS,SAAY,GAAA;AAC1B,EAAM,MAAA,OAAA,GAAU,OAAO,aAAa,CAAA;AAEpC,EAAA,IAAI,CAAC,OAAS,EAAA;AACZ,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KACF;AAAA;AAGF,EAAO,OAAA,OAAA,CAAQ,OAAQ,CAAA,KAAA,EAAO,EAAE,CAAA;AAClC;AAEO,SAAS,YAAe,GAAA;AAC7B,EAAA,MAAM,MAAM,MAAO,CAAA,YAAY,CAAK,IAAA,MAAA,CAAO,UAAU,CAAK,IAAA,EAAA;AAC1D,EAAA,OAAO,CAAC,YAAc,EAAA,MAAM,CAAE,CAAA,OAAA,CAAQ,GAAG,CAAI,GAAA,EAAA;AAC/C;AAEO,SAAS,iCAAiC,OAE9C,EAAA;AACD,EAAI,IAAA,MAAA,CAAO,YAAY,CAAG,EAAA;AAKxB,IAAA,IAAI,CAAC,YAAA,EAAkB,IAAA,MAAA,CAAO,YAAY,CAAG,EAAA;AAC3C,MAAA,OAAO,WAAW,MAAO,CAAA,YAAY,CAAC,CAAG,CAAA,CAAA,OAAA,CAAQ,OAAO,EAAE,CAAA;AAAA;AAG5D,IAAM,MAAA,aAAA,GAAgB,MAAO,CAAA,+BAA+B,CAAK,IAAA,EAAA;AACjE,IAAA,IAAI,cAAkB,IAAA,aAAA,CAA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T,MAAA,KAAK,UAAW,EAAA;AAAA;AAClB,GACF,EAAG,EAAE,CAAA;AAEL,EAAO,OAAA,IAAA;AACT","file":"index.mjs","sourcesContent":["// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\n/**\n * Gets environment variable, prioritizing the NEXT_PUBLIC_ prefixed version\n */\nfunction getEnv(name: string): string | undefined {\n  return process.env[`NEXT_PUBLIC_${name}`] || process.env[name]\n}\n\nexport function orySdkUrl() {\n  const baseUrl = getEnv(\"ORY_SDK_URL\")\n\n  if (!baseUrl) {\n    throw new Error(\n      \"You need to set environment variable `NEXT_PUBLIC_ORY_SDK_URL` to your Ory Network SDK URL.\",\n    )\n  }\n\n  return baseUrl.replace(/\\/$/, \"\")\n}\n\nexport function isProduction() {\n  const env = getEnv(\"VERCEL_ENV\") || getEnv(\"NODE_ENV\") || \"\"\n  return [\"production\", \"prod\"].indexOf(env) > -1\n}\n\nexport function guessPotentiallyProxiedOrySdkUrl(options?: {\n  knownProxiedUrl?: string\n}) {\n  if (getEnv(\"VERCEL_ENV\")) {\n    // We are in vercel\n\n    // The domain name of the generated deployment URL. Example: *.vercel.app\n    // This is only available for preview deployments on Vercel.\n    if (!isProduction() && getEnv(\"VERCEL_URL\")) {\n      return `https://${getEnv(\"VERCEL_URL\")}`.replace(/\\/$/, \"\")\n    }\n\n    const productionUrl = getEnv(\"VERCEL_PROJECT_PRODUCTION_URL\") || \"\"\n    if (isProduction() && productionUrl.indexOf(\"vercel.app\") > -1) {\n      // This is a production deployment on Vercel without a custom domain, so we use the vercel app domain.\n      return `https://${productionUrl}`.replace(/\\/$/, \"\")\n    }\n\n    // This is sometimes set by the render server.\n    if (process.env[\"__NEXT_PRIVATE_ORIGIN\"]) {\n      return process.env[\"__NEXT_PRIVATE_ORIGIN\"].replace(/\\/$/, \"\")\n    }\n  }\n\n  if (isProduction()) {\n    // In production, we use the production custom domain\n    return orySdkUrl()\n  }\n\n  // Try to use window location\n  if (typeof window !== \"undefined\") {\n    return window.location.origin\n  }\n\n  if (options?.knownProxiedUrl) {\n    return options.knownProxiedUrl\n  }\n\n  // We tried everything. Let's use the SDK URL.\n  const final = orySdkUrl()\n  console.warn(\n    `Unable to determine a suitable SDK URL for setting up the Next.js integration of Ory Elements. Will proceed using default Ory SDK URL \"${final}\". This is likely not what you want for local development and your authentication and login may not work.`,\n  )\n\n  return final\n}\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\nimport { Configuration, FrontendApi } from \"@ory/client-fetch\"\n\nimport { guessPotentiallyProxiedOrySdkUrl } from \"../utils/sdk\"\n\nexport const clientSideFrontendClient = () =>\n  new FrontendApi(\n    new Configuration({\n      headers: {\n        Accept: \"application/json\",\n      },\n      credentials: \"include\",\n      basePath: guessPotentiallyProxiedOrySdkUrl({\n        knownProxiedUrl: window.location.origin,\n      }),\n    }),\n  )\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { FlowType, OnRedirectHandler } from \"@ory/client-fetch\"\nimport { guessPotentiallyProxiedOrySdkUrl } from \"../utils/sdk\"\nimport { useRouter } from \"next/router\"\n\nexport function onValidationError<T>(value: T): T {\n  return value\n}\n\nexport const toBrowserEndpointRedirect = (\n  params: URLSearchParams,\n  flowType: FlowType,\n) =>\n  guessPotentiallyProxiedOrySdkUrl({\n    knownProxiedUrl: window.location.origin,\n  }) +\n  \"/self-service/\" +\n  flowType.toString() +\n  \"/browser?\" +\n  new URLSearchParams(params).toString()\n\nexport const handleRestartFlow =\n  (searchParams: URLSearchParams, flowType: FlowType) => () => {\n    window.location.assign(toBrowserEndpointRedirect(searchParams, flowType))\n  }\n\nexport function useOnRedirect(): OnRedirectHandler {\n  const router = useRouter()\n  return (url: string, external: boolean) => {\n    if (external) {\n      window.location.assign(url)\n    } else {\n      void router.push(url)\n    }\n  }\n}\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { OryMiddlewareOptions } from \"src/middleware/middleware\"\nimport { orySdkUrl } from \"./sdk\"\nimport { joinUrlPaths } from \"./utils\"\n\nexport function rewriteUrls(\n  source: string,\n  matchBaseUrl: string,\n  selfUrl: string,\n  config: OryMiddlewareOptions,\n) {\n  for (const [_, [matchPath, replaceWith]] of [\n    // TODO load these dynamically from the project config\n\n    // Old AX routes\n    [\"/ui/recovery\", config.project?.recovery_ui_url],\n    [\"/ui/registration\", config.project?.registration_ui_url],\n    [\"/ui/login\", config.project?.login_ui_url],\n    [\"/ui/verification\", config.project?.verification_ui_url],\n    [\"/ui/settings\", config.project?.settings_ui_url],\n    [\"/ui/welcome\", config.project?.default_redirect_url],\n\n    // New AX routes\n    [\"/recovery\", config.project?.recovery_ui_url],\n    [\"/registration\", config.project?.registration_ui_url],\n    [\"/login\", config.project?.login_ui_url],\n    [\"/verification\", config.project?.verification_ui_url],\n    [\"/settings\", config.project?.settings_ui_url],\n  ].entries()) {\n    const match = joinUrlPaths(matchBaseUrl, matchPath || \"\")\n    if (replaceWith && source.startsWith(match)) {\n      source = source.replaceAll(\n        match,\n        new URL(replaceWith, selfUrl).toString(),\n      )\n    }\n  }\n  return source.replaceAll(\n    matchBaseUrl.replace(/\\/$/, \"\"),\n    new URL(selfUrl).toString().replace(/\\/$/, \"\"),\n  )\n}\n\n/**\n * Rewrites Ory SDK URLs in JSON responses (objects, arrays, strings) with the provided proxy URL.\n *\n * If `proxyUrl` is provided, the SDK URL is replaced with the proxy URL.\n *\n * @param obj - The object to rewrite\n * @param proxyUrl - The proxy URL to replace the SDK URL with\n */\nexport function rewriteJsonResponse<T extends object>(\n  obj: T,\n  proxyUrl?: string,\n): T {\n  return Object.fromEntries(\n    Object.entries(obj)\n      .filter(([_, value]) => value !== undefined)\n      .map(([key, value]) => {\n        if (Array.isArray(value)) {\n          // Recursively process each item in the array\n          return [\n            key,\n            value\n              .map((item) => {\n                if (typeof item === \"object\" && item !== null) {\n                  // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n                  return rewriteJsonResponse(item, proxyUrl)\n                } else if (typeof item === \"string\" && proxyUrl) {\n                  return item.replaceAll(orySdkUrl(), proxyUrl)\n                }\n                // eslint-disable-next-line @typescript-eslint/no-unsafe-return\n                return item\n              })\n              .filter((item) => item !== undefined),\n          ]\n        } else if (typeof value === \"object\" && value !== null) {\n          // Recursively remove undefined in nested objects\n          return [key, rewriteJsonResponse(value, proxyUrl)]\n        } else if (typeof value === \"string\" && proxyUrl) {\n          // Replace SDK URL with the provided proxy URL\n          return [key, value.replaceAll(orySdkUrl(), proxyUrl)]\n        }\n        return [key, value]\n      }),\n  ) as T\n}\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\nimport { serialize, SerializeOptions } from \"cookie\"\nimport { parse, splitCookiesString } from \"set-cookie-parser\"\n\nimport { ApiResponse } from \"@ory/client-fetch\"\nimport { OryMiddlewareOptions } from \"src/middleware/middleware\"\nimport { FlowParams, QueryParams } from \"../types\"\nimport { guessCookieDomain } from \"./cookie\"\nimport { defaultForwardedHeaders } from \"./headers\"\nimport { rewriteJsonResponse } from \"./rewrite\"\n\nexport function onValidationError<T>(value: T): T {\n  return value\n}\n\nexport async function toFlowParams(\n  params: QueryParams,\n  getCookieHeader: () => Promise<string | undefined>,\n): Promise<FlowParams> {\n  return {\n    id: params[\"flow\"]?.toString() ?? \"\",\n    cookie: await getCookieHeader(),\n    return_to: params[\"return_to\"]?.toString() ?? \"\",\n  }\n}\nexport function processSetCookieHeaders(\n  protocol: string,\n  fetchResponse: Response,\n  options: OryMiddlewareOptions,\n  requestHeaders: Headers,\n) {\n  const isTls =\n    protocol === \"https:\" || requestHeaders.get(\"x-forwarded-proto\") === \"https\"\n\n  const forwarded = requestHeaders.get(\"x-forwarded-host\")\n  const host = forwarded ? forwarded : requestHeaders.get(\"host\")\n  const domain =\n    host && !options.forceCookieDomain\n      ? guessCookieDomain(host ?? \"\")\n      : options.forceCookieDomain\n\n  return parse(\n    splitCookiesString(fetchResponse.headers.get(\"set-cookie\") || \"\"),\n  )\n    .map((cookie) => ({\n      ...cookie,\n      domain,\n      secure: isTls,\n      encode: (v: string) => v,\n    }))\n    .map(({ value, name, ...options }) =>\n      serialize(name, value, options as SerializeOptions),\n    )\n}\n\nexport function filterRequestHeaders(\n  headers: Headers,\n  forwardAdditionalHeaders?: string[],\n): Headers {\n  const filteredHeaders = new Headers()\n\n  headers.forEach((value, key) => {\n    const isValid =\n      defaultForwardedHeaders.includes(key) ||\n      (forwardAdditionalHeaders ?? []).includes(key)\n    if (isValid) filteredHeaders.set(key, value)\n  })\n\n  return filteredHeaders\n}\n\nexport function joinUrlPaths(baseUrl: string, relativeUrl: string): string {\n  const base = new URL(baseUrl)\n  const relative = new URL(relativeUrl, baseUrl)\n\n  relative.pathname =\n    base.pathname.replace(/\\/$/, \"\") +\n    \"/\" +\n    relative.pathname.replace(/^\\//, \"\")\n\n  return new URL(relative.toString(), baseUrl).href\n}\n\nexport function toValue<T extends object>(res: ApiResponse<T>): Promise<T> {\n  // Remove all undefined values from the response (array and object) using lodash:\n  // Remove all (nested) undefined values from the response using lodash\n  return res.value().then((v) => rewriteJsonResponse(v))\n}\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { FlowType, handleFlowError } from \"@ory/client-fetch\"\nimport { useEffect, useState } from \"react\"\nimport { useRouter } from \"next/router\"\nimport { useSearchParams } from \"next/navigation\"\nimport { handleRestartFlow, onValidationError, useOnRedirect } from \"./utils\"\nimport { toValue } from \"../utils/utils\"\nimport * as runtime from \"@ory/client-fetch/src/runtime\"\n\ninterface Flow {\n  id: string\n}\n\nexport function createUseFlowFactory<T extends Flow>(\n  flowType: FlowType,\n  createFlow: (params: URLSearchParams) => Promise<runtime.ApiResponse<T>>,\n  getFlow: (id: string) => Promise<runtime.ApiResponse<T>>,\n): () => T | null | void {\n  return () => {\n    const [flow, setFlow] = useState<T>()\n    const router = useRouter()\n    const searchParams = useSearchParams()\n    const onRestartFlow = handleRestartFlow(searchParams, flowType)\n    const onRedirect = useOnRedirect()\n\n    const errorHandler = handleFlowError({\n      onValidationError,\n      onRestartFlow,\n      onRedirect,\n    })\n\n    const handleSetFlow = async (flow: T) => {\n      setFlow(flow)\n\n      // Use the router to update the `flow` search parameter only\n      await router.replace({\n        query: { flow: flow.id },\n      })\n      return\n    }\n\n    useEffect(() => {\n      const id = searchParams.get(\"flow\")\n\n      // If the router is not ready yet, or we already have a flow, do nothing.\n      if (!router.isReady || flow !== undefined) {\n        return\n      }\n\n      if (!id) {\n        createFlow(searchParams)\n          .then(toValue)\n          .then(handleSetFlow)\n          .catch(errorHandler)\n        return\n      }\n\n      getFlow(id).then(toValue).then(handleSetFlow).catch(errorHandler)\n    }, [searchParams, router, router.isReady, flow])\n\n    return flow\n  }\n}\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { clientSideFrontendClient } from \"./client\"\nimport { createUseFlowFactory } from \"./flow\"\nimport { FlowType } from \"@ory/client-fetch\"\n\n/**\n * A client side hook to create a registration flow.\n *\n * @returns A registration flow\n * @public\n * @function\n * @group Hooks\n */\nexport const useRegistrationFlow = createUseFlowFactory(\n  FlowType.Registration,\n  (params: URLSearchParams) => {\n    return clientSideFrontendClient().createBrowserRegistrationFlowRaw({\n      returnTo: params.get(\"return_to\") ?? undefined,\n      loginChallenge: params.get(\"registration_challenge\") ?? undefined,\n      afterVerificationReturnTo:\n        params.get(\"after_verification_return_to\") ?? undefined,\n      organization: params.get(\"organization\") ?? undefined,\n    })\n  },\n  (id) => clientSideFrontendClient().getRegistrationFlowRaw({ id }),\n)\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { clientSideFrontendClient } from \"./client\"\nimport { createUseFlowFactory } from \"./flow\"\nimport { FlowType } from \"@ory/client-fetch\"\n\n/**\n * A client side hook to create a verification flow.\n *\n * @returns A verification flow\n * @public\n * @function\n * @group Hooks\n */\nexport const useVerificationFlow = createUseFlowFactory(\n  FlowType.Verification,\n  (params: URLSearchParams) => {\n    return clientSideFrontendClient().createBrowserVerificationFlowRaw({\n      returnTo: params.get(\"return_to\") ?? undefined,\n    })\n  },\n  (id) => clientSideFrontendClient().getVerificationFlowRaw({ id }),\n)\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { clientSideFrontendClient } from \"./client\"\nimport { createUseFlowFactory } from \"./flow\"\nimport { FlowType } from \"@ory/client-fetch\"\n\n/**\n * A client side hook to create a recovery flow.\n *\n * @returns A recovery flow\n * @public\n * @function\n * @group Hooks\n */\nexport const useRecoveryFlow = createUseFlowFactory(\n  FlowType.Recovery,\n  (params: URLSearchParams) => {\n    return clientSideFrontendClient().createBrowserRecoveryFlowRaw({\n      returnTo: params.get(\"return_to\") ?? undefined,\n    })\n  },\n  (id) => clientSideFrontendClient().getRecoveryFlowRaw({ id }),\n)\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { clientSideFrontendClient } from \"./client\"\nimport { createUseFlowFactory } from \"./flow\"\nimport { FlowType } from \"@ory/client-fetch\"\n\n/**\n * A client side hook to create a login flow.\n *\n * @returns A login flow\n * @public\n * @function\n * @group Hooks\n */\nexport const useLoginFlow = createUseFlowFactory(\n  FlowType.Login,\n  (params: URLSearchParams) => {\n    return clientSideFrontendClient().createBrowserLoginFlowRaw({\n      refresh: params.get(\"refresh\") === \"true\",\n      aal: params.get(\"aal\") ?? undefined,\n      returnTo: params.get(\"return_to\") ?? undefined,\n      cookie: params.get(\"cookie\") ?? undefined,\n      loginChallenge: params.get(\"login_challenge\") ?? undefined,\n      organization: params.get(\"organization\") ?? undefined,\n      via: params.get(\"via\") ?? undefined,\n    })\n  },\n  (id) => clientSideFrontendClient().getLoginFlowRaw({ id }),\n)\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { FlowType } from \"@ory/client-fetch\"\nimport { createUseFlowFactory } from \"./flow\"\nimport { clientSideFrontendClient } from \"./client\"\n\n/**\n * A client side hook to create a settings flow.\n *\n * @returns A settings flow\n * @public\n * @function\n * @group Hooks\n */\nexport const useSettingsFlow = createUseFlowFactory(\n  FlowType.Settings,\n  (params: URLSearchParams) => {\n    return clientSideFrontendClient().createBrowserSettingsFlowRaw({\n      returnTo: params.get(\"return_to\") ?? undefined,\n      cookie: params.get(\"cookie\") ?? undefined,\n    })\n  },\n  (id) => clientSideFrontendClient().getSettingsFlowRaw({ id }),\n)\n","// Copyright © 2024 Ory Corp\n// SPDX-License-Identifier: Apache-2.0\n\nimport { LogoutFlow } from \"@ory/client-fetch\"\nimport { clientSideFrontendClient } from \"./client\"\nimport { useEffect, useState } from \"react\"\n\n/**\n * A client side hook to create a logout flow.\n *\n * @returns A logout flow\n * @public\n * @group Hooks\n */\nexport function useLogoutFlow() {\n  const [flow, setFlow] = useState<LogoutFlow | undefined>(undefined)\n\n  const createFlow = async () => {\n    const flow = await clientSideFrontendClient().createBrowserLogoutFlow({})\n    setFlow(flow)\n  }\n\n  useEffect(() => {\n    if (!flow) {\n      void createFlow()\n    }\n  }, [])\n\n  return flow\n}\n"]}