-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
136 additions
and
32 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import React from "react"; | ||
import Link from "next/link"; | ||
import {useSession} from "@ory/nextjs/hooks"; | ||
import React from "react" | ||
import Link from "next/link" | ||
import { useSession } from "@ory/nextjs/hooks" | ||
|
||
export default function Home() { | ||
const session = useSession() | ||
if (session) { | ||
return "Hello: " + session.identity?.traits.email | ||
} | ||
return ( | ||
<p> | ||
Not authenticated, please <Link href={"/ui/login"}>log in</Link>. | ||
</p> | ||
<p> | ||
Not authenticated, please <Link href={"/ui/login"}>log in</Link>. | ||
</p> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/** | ||
* This function returns the base URL of the Ory SDK as set by environment variables `NEXT_PUBLIC_ORY_SDK_URL` or `ORY_SDK_URL`. | ||
*/ | ||
export function orySdkUrl() { | ||
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 `NEXT_PUBLIC_ORY_SDK_URL` or if you don't use Next.js `ORY_SDK_URL` to your Ory Network SDK URL.", | ||
) | ||
} | ||
|
||
return baseUrl.replace(/\/$/, "") | ||
} | ||
|
||
/** | ||
* This function returns whether the current environment is a production environment. | ||
*/ | ||
export function isProduction() { | ||
return ( | ||
["production", "prod"].indexOf( | ||
process.env["VERCEL_ENV"] || process.env["NODE_ENV"] || "", | ||
) > -1 | ||
) | ||
} | ||
|
||
/** | ||
* This function returns the Ory SDK URL. If the environment is not production, it tries to guess the SDK URL based on the environment variables, assuming | ||
* that Ory APIs are proxied through the same domain as the application. | ||
* | ||
* Currently, this is only tested for Vercel deployments. | ||
* | ||
* @param options | ||
*/ | ||
export function guessPotentiallyProxiedOrySdkUrl(options?: { | ||
knownProxiedUrl?: string | ||
}) { | ||
if (isProduction()) { | ||
// In production, we use the production custom domain | ||
return orySdkUrl() | ||
} | ||
|
||
if (process.env["VERCEL_ENV"]) { | ||
// We are in vercel | ||
|
||
// The domain name of the generated deployment URL. Example: *.vercel.app. The value does not include the protocol scheme https://. | ||
// | ||
// This is only available for preview deployments on Vercel. | ||
if (!isProduction() && process.env["VERCEL_URL"]) { | ||
return `https://${process.env["VERCEL_URL"]}`.replace(/\/$/, "") | ||
} | ||
|
||
// This is sometimes set by the render server. | ||
if (process.env["__NEXT_PRIVATE_ORIGIN"]) { | ||
return process.env["__NEXT_PRIVATE_ORIGIN"].replace(/\/$/, "") | ||
} | ||
} | ||
|
||
// Unable to figure out the SDK URL. Either because we are not using Vercel or because we are on a local machine. | ||
// Let's try to use the window location. | ||
if (typeof window !== "undefined") { | ||
return window.location.origin | ||
} | ||
|
||
if (options?.knownProxiedUrl) { | ||
return options.knownProxiedUrl | ||
} | ||
|
||
// We tried everything. Let's use the SDK URL. | ||
const final = orySdkUrl() | ||
console.warn( | ||
`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.`, | ||
) | ||
|
||
return final | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
// Copyright © 2024 Ory Corp | ||
// SPDX-License-Identifier: Apache-2.0 | ||
"use client" | ||
export { useSession } from "./useSession" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters