-
-
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.
feat: add nextjs example and supporting libraries
- Loading branch information
Showing
30 changed files
with
1,252 additions
and
1 deletion.
There are no files selected for viewing
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 @@ | ||
ORY_SDK_URL=https://playground.projects.oryapis.com |
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,3 @@ | ||
{ | ||
"extends": ["next/core-web-vitals", "next/typescript"] | ||
} |
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,36 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
/node_modules | ||
/.pnp | ||
.pnp.js | ||
.yarn/install-state.gz | ||
|
||
# testing | ||
/coverage | ||
|
||
# next.js | ||
/.next/ | ||
/out/ | ||
|
||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
|
||
# vercel | ||
.vercel | ||
|
||
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts |
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,36 @@ | ||
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app). | ||
|
||
## Getting Started | ||
|
||
First, run the development server: | ||
|
||
```bash | ||
npm run dev | ||
# or | ||
yarn dev | ||
# or | ||
pnpm dev | ||
# or | ||
bun dev | ||
``` | ||
|
||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result. | ||
|
||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file. | ||
|
||
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel. | ||
|
||
## Learn More | ||
|
||
To learn more about Next.js, take a look at the following resources: | ||
|
||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API. | ||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial. | ||
|
||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome! | ||
|
||
## Deploy on Vercel | ||
|
||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js. | ||
|
||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details. |
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,3 @@ | ||
import AuthLayout from "@/routers/app/layout"; | ||
|
||
export default AuthLayout |
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,20 @@ | ||
import { Login } from "@ory/elements-react/theme" | ||
import config from "@/app/(ory)/config" | ||
import { getOrCreateLoginFlow } from "@/routers/app/login" | ||
import { newFrontendClient } from "@/fetch/sdk" | ||
|
||
const client = newFrontendClient() | ||
|
||
export default async function LoginPage({ | ||
searchParams, | ||
}: { | ||
searchParams: URLSearchParams | ||
}) { | ||
const flow = await getOrCreateLoginFlow(searchParams, client) | ||
|
||
if (!flow) { | ||
return null | ||
} | ||
|
||
return <Login flow={flow} config={config} /> | ||
} |
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,29 @@ | ||
import { OryClientConfiguration } from "@ory/elements-react" | ||
|
||
let sdkUrl = process.env.ORY_SDK_URL || "" | ||
|
||
export function setSdkUrl(url: string) { | ||
sdkUrl = url | ||
} | ||
|
||
export function getSdkUrl() { | ||
return sdkUrl | ||
} | ||
|
||
const config: OryClientConfiguration = { | ||
name: "Ory Elements example app", | ||
sdk: { | ||
url: getSdkUrl(), | ||
}, | ||
project: { | ||
registration_enabled: true, | ||
verification_enabled: true, | ||
recovery_enabled: true, | ||
recovery_ui_url: "/ui/recovery", | ||
registration_ui_url: "/ui/registration", | ||
verification_ui_url: "/ui/verification", | ||
login_ui_url: "/ui/login", | ||
}, | ||
} | ||
|
||
export default config |
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,3 @@ | ||
import AuthLayout from "@/routers/app/layout"; | ||
|
||
export default AuthLayout |
3 changes: 3 additions & 0 deletions
3
examples/nextjs-app-router/app/(ory)/self-service/[[...paths]]/route.tsx
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,3 @@ | ||
import { createApiHandler } from "@/routers/app/proxy" | ||
|
||
export const { GET, PUT, POST, config } = createApiHandler() |
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,3 @@ | ||
import { createApiHandler } from "@/routers/app/proxy" | ||
|
||
export const { GET, PUT, POST, config } = createApiHandler() |
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,15 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; | ||
|
||
body { | ||
color: var(--foreground); | ||
background: var(--background); | ||
font-family: Arial, Helvetica, sans-serif; | ||
} | ||
|
||
@layer utilities { | ||
.text-balance { | ||
text-wrap: balance; | ||
} | ||
} |
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,16 @@ | ||
import "./globals.css" | ||
import React, { Suspense, ReactNode } from "react" | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: Readonly<{ | ||
children: ReactNode | ||
}>) { | ||
return ( | ||
<html lang="en"> | ||
<body className={`antialiased overflow-hidden`}> | ||
<Suspense>{children}</Suspense> | ||
</body> | ||
</html> | ||
) | ||
} |
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,21 @@ | ||
import { Configuration, FrontendApi } from "@ory/client-fetch" | ||
|
||
let sdkUrl = process.env.ORY_SDK_URL || "" | ||
|
||
export function setSdkUrl(url: string) { | ||
sdkUrl = url | ||
} | ||
|
||
export function getSdkUrl() { | ||
return sdkUrl | ||
} | ||
|
||
export function newFrontendClient() { | ||
const config = new Configuration({ | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
basePath: getSdkUrl(), | ||
}) | ||
return new FrontendApi(config) | ||
} |
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,4 @@ | ||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
export default nextConfig; |
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,32 @@ | ||
{ | ||
"name": "nextjs-app-router", | ||
"version": "0.1.0", | ||
"private": true, | ||
"scripts": { | ||
"dev": "next dev", | ||
"build": "next build", | ||
"start": "next start", | ||
"lint": "next lint" | ||
}, | ||
"dependencies": { | ||
"@types/set-cookie-parser": "^2.4.10", | ||
"cookie": "^1.0.1", | ||
"next": "14.2.15", | ||
"react": "^18", | ||
"react-dom": "^18", | ||
"set-cookie-parser": "^2.7.0", | ||
"tldjs": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"@ory/client-fetch": "^1.15.6", | ||
"@types/node": "^20", | ||
"@types/react": "^18", | ||
"@types/react-dom": "^18", | ||
"@types/tldjs": "^2.3.4", | ||
"eslint": "^8", | ||
"eslint-config-next": "14.2.15", | ||
"postcss": "^8", | ||
"tailwindcss": "^3.4.1", | ||
"typescript": "^5" | ||
} | ||
} |
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,5 @@ | ||
export const isVercel = process.env.VERCEL === "1" | ||
|
||
export function isVercelAppDeployment() { | ||
return (process.env.VERCEL_URL || "").includes(".vercel.app") | ||
} |
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,8 @@ | ||
/** @type {import('postcss-load-config').Config} */ | ||
const config = { | ||
plugins: { | ||
tailwindcss: {}, | ||
}, | ||
}; | ||
|
||
export default config; |
13 changes: 13 additions & 0 deletions
13
examples/nextjs-app-router/routers/app/common/default-forwarded-headers.ts
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,13 @@ | ||
export const defaultForwardedHeaders = [ | ||
"accept", | ||
"accept-charset", | ||
"accept-encoding", | ||
"accept-language", | ||
"authorization", | ||
"cache-control", | ||
"content-type", | ||
"cookie", | ||
"host", | ||
"user-agent", | ||
"referer", | ||
] |
16 changes: 16 additions & 0 deletions
16
examples/nextjs-app-router/routers/app/common/get-base-url.ts
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,16 @@ | ||
import { type CreateApiHandlerOptions } from "../type/create-api-handler-options" | ||
|
||
export function getBaseUrl(options: CreateApiHandlerOptions) { | ||
let baseUrl = "" | ||
|
||
if (process.env.ORY_SDK_URL) { | ||
baseUrl = process.env.ORY_SDK_URL | ||
} | ||
|
||
if (options.orySdkUrl) { | ||
baseUrl = options.orySdkUrl | ||
} | ||
|
||
return baseUrl.replace(/\/$/, "") | ||
} | ||
export { CreateApiHandlerOptions } |
59 changes: 59 additions & 0 deletions
59
examples/nextjs-app-router/routers/app/common/get-cookie-domain.test.ts
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,59 @@ | ||
import { guessCookieDomain } from "./get-cookie-domain" | ||
|
||
describe("cookie guesser", () => { | ||
test("uses force domain", () => { | ||
expect( | ||
guessCookieDomain("https://localhost", { | ||
forceCookieDomain: "some-domain", | ||
}), | ||
).toEqual("some-domain") | ||
}) | ||
|
||
test("does not use any guessing domain", () => { | ||
expect(guessCookieDomain("https://localhost", {})).toEqual(undefined) | ||
}) | ||
|
||
test("is not confused by invalid data", () => { | ||
expect(guessCookieDomain("5qw5tare4g", {})).toEqual(undefined) | ||
expect(guessCookieDomain("https://123.123.123.123.123", {})).toEqual( | ||
undefined, | ||
) | ||
}) | ||
|
||
test("is not confused by IP", () => { | ||
expect(guessCookieDomain("https://123.123.123.123", {})).toEqual(undefined) | ||
expect( | ||
guessCookieDomain("https://2001:0db8:0000:0000:0000:ff00:0042:8329", {}), | ||
).toEqual(undefined) | ||
}) | ||
|
||
test("returns undefined for public suffix", () => { | ||
expect(guessCookieDomain("https://asdf.vercel.app", {})).toEqual(undefined) | ||
}) | ||
|
||
test("uses TLD", () => { | ||
expect(guessCookieDomain("https://foo.localhost", {})).toEqual( | ||
"foo.localhost", | ||
) | ||
|
||
expect(guessCookieDomain("https://foo.localhost:1234", {})).toEqual( | ||
"foo.localhost", | ||
) | ||
|
||
expect( | ||
guessCookieDomain( | ||
"https://spark-public.s3.amazonaws.com/dataanalysis/loansData.csv", | ||
{}, | ||
), | ||
).toEqual("spark-public.s3.amazonaws.com") | ||
|
||
expect(guessCookieDomain("spark-public.s3.amazonaws.com", {})).toEqual( | ||
"spark-public.s3.amazonaws.com", | ||
) | ||
|
||
expect(guessCookieDomain("https://localhost/123", {})).toEqual("localhost") | ||
expect(guessCookieDomain("https://localhost:1234/123", {})).toEqual( | ||
"localhost", | ||
) | ||
}) | ||
}) |
29 changes: 29 additions & 0 deletions
29
examples/nextjs-app-router/routers/app/common/get-cookie-domain.ts
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,29 @@ | ||
import tldjs from "tldjs" | ||
import { CreateApiHandlerOptions } from "./get-base-url" | ||
import { isVercelAppDeployment } from "@/platforms/vercel" | ||
|
||
export function guessCookieDomain( | ||
url: string | undefined, | ||
options: CreateApiHandlerOptions, | ||
) { | ||
if (!url || options.forceCookieDomain) { | ||
return options.forceCookieDomain | ||
} | ||
|
||
const parsed = tldjs.parse(url || "") | ||
|
||
if (!parsed.isValid || parsed.isIp) { | ||
return undefined | ||
} | ||
|
||
if (parsed.publicSuffix) { | ||
// We can't set the cookie for public domain suffixes. | ||
return undefined | ||
} | ||
|
||
if (!parsed.domain) { | ||
return parsed.hostname | ||
} | ||
|
||
return parsed.domain | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/nextjs-app-router/routers/app/common/process-location-header.ts
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,11 @@ | ||
export function processLocationHeader( | ||
locationHeaderValue: string, | ||
baseUrl: string, | ||
basePath: string = "", | ||
) { | ||
if (locationHeaderValue.startsWith(baseUrl)) { | ||
return locationHeaderValue.replace(baseUrl, basePath.replace(/\/$/, "")) | ||
} | ||
|
||
return locationHeaderValue | ||
} |
Oops, something went wrong.