Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: example #237

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/nextjs-app-router/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ORY_SDK_URL=https://playground.projects.oryapis.com
3 changes: 3 additions & 0 deletions examples/nextjs-app-router/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["next/core-web-vitals", "next/typescript"]
}
36 changes: 36 additions & 0 deletions examples/nextjs-app-router/.gitignore
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
36 changes: 36 additions & 0 deletions examples/nextjs-app-router/README.md
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.
3 changes: 3 additions & 0 deletions examples/nextjs-app-router/app/(auth)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AuthLayout from "@/routers/app/layout";

export default AuthLayout
20 changes: 20 additions & 0 deletions examples/nextjs-app-router/app/(auth)/login/page.tsx
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} />
}
29 changes: 29 additions & 0 deletions examples/nextjs-app-router/app/(ory)/config.ts
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
3 changes: 3 additions & 0 deletions examples/nextjs-app-router/app/(ory)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AuthLayout from "@/routers/app/layout";

export default AuthLayout
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()
3 changes: 3 additions & 0 deletions examples/nextjs-app-router/app/(ory)/ui/[...paths]/route.tsx
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()
15 changes: 15 additions & 0 deletions examples/nextjs-app-router/app/globals.css
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;
}
}
16 changes: 16 additions & 0 deletions examples/nextjs-app-router/app/layout.tsx
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>
)
}
21 changes: 21 additions & 0 deletions examples/nextjs-app-router/fetch/sdk.ts
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)
}
4 changes: 4 additions & 0 deletions examples/nextjs-app-router/next.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
32 changes: 32 additions & 0 deletions examples/nextjs-app-router/package.json
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"
}
}
5 changes: 5 additions & 0 deletions examples/nextjs-app-router/platforms/vercel/index.ts
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")
}
8 changes: 8 additions & 0 deletions examples/nextjs-app-router/postcss.config.mjs
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;
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 examples/nextjs-app-router/routers/app/common/get-base-url.ts
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 }
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 examples/nextjs-app-router/routers/app/common/get-cookie-domain.ts
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
}
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
}
Loading
Loading