Skip to content

Commit

Permalink
chore: synchronize workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
aeneasr committed Oct 15, 2024
1 parent 2d56b78 commit f34f04d
Show file tree
Hide file tree
Showing 23 changed files with 1,172 additions and 1 deletion.
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.
19 changes: 19 additions & 0 deletions examples/nextjs-app-router/app/(ory)/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { OryClientConfiguration } from "@ory/elements-react"

const config: OryClientConfiguration = {
name: "Ory Elements example app",
sdk: {
url: process.env.ORY_SDK_URL || ''
},
project:{
registration_enabled: true,
verification_enabled: true,
recovery_enabled: true,
recovery_ui_url: '/recovery',
registration_ui_url: '/registration',
verification_ui_url: '/verification',
login_ui_url: '/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
11 changes: 11 additions & 0 deletions examples/nextjs-app-router/app/(ory)/sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Configuration, FrontendApi } from "@ory/client-fetch"

export function newSDK() {
const config = new Configuration({
headers: {
Accept: "application/json",
},
basePath: process.env.ORY_SDK_URL,
})
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;
30 changes: 30 additions & 0 deletions examples/nextjs-app-router/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"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"
},
"devDependencies": {
"@ory/client-fetch": "^1.15.6",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"eslint": "^8",
"eslint-config-next": "14.2.15",
"postcss": "^8",
"tailwindcss": "^3.4.1",
"typescript": "^5"
}
}
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",
]
26 changes: 26 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,26 @@
import { type CreateApiHandlerOptions } from "../type/create-api-handler-options"

export function getBaseUrl(options: CreateApiHandlerOptions) {
let baseUrl = options.fallbackToPlayground
? "https://playground.projects.oryapis.com/"
: ""

if (process.env.ORY_SDK_URL) {
baseUrl = process.env.ORY_SDK_URL
}

if (process.env.ORY_KRATOS_URL) {
baseUrl = process.env.ORY_KRATOS_URL
}

if (process.env.ORY_SDK_URL && process.env.ORY_KRATOS_URL) {
throw new Error("Only one of ORY_SDK_URL or ORY_KRATOS_URL can be set.")
}

if (options.apiBaseUrlOverride) {
baseUrl = options.apiBaseUrlOverride
}

return baseUrl.replace(/\/$/, "")
}
export { CreateApiHandlerOptions }
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { guessCookieDomain } from "./get-cookie-domain"

describe("cookie guesser", () => {
test("uses force domain", async () => {
expect(
guessCookieDomain("https://localhost", {
forceCookieDomain: "some-domain",
}),
).toEqual("some-domain")
})

test("does not use any guessing domain", async () => {
expect(
guessCookieDomain("https://localhost", {
dontUseTldForCookieDomain: true,
}),
).toEqual(undefined)
})

test("is not confused by invalid data", async () => {
expect(
guessCookieDomain("5qw5tare4g", {
dontUseTldForCookieDomain: true,
}),
).toEqual(undefined)
expect(
guessCookieDomain("https://123.123.123.123.123", {
dontUseTldForCookieDomain: true,
}),
).toEqual(undefined)
})

test("is not confused by IP", async () => {
expect(
guessCookieDomain("https://123.123.123.123", {
dontUseTldForCookieDomain: true,
}),
).toEqual(undefined)
expect(
guessCookieDomain("https://2001:0db8:0000:0000:0000:ff00:0042:8329", {
dontUseTldForCookieDomain: true,
}),
).toEqual(undefined)
})

test("uses TLD", async () => {
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",
)
})
})
27 changes: 27 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,27 @@
import tldjs from "tldjs"
import { CreateApiHandlerOptions } from "./get-base-url"

export function guessCookieDomain(
url: string | undefined,
options: CreateApiHandlerOptions,
) {
if (!url || options.forceCookieDomain) {
return options.forceCookieDomain
}

if (options.dontUseTldForCookieDomain) {
return undefined
}

const parsed = tldjs.parse(url || "")

if (!parsed.isValid || parsed.isIp) {
return undefined
}

if (!parsed.domain) {
return parsed.hostname
}

return parsed.domain
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export function processLocationHeader(
locationHeaderValue: string,
baseUrl: string,
) {
if (locationHeaderValue.startsWith(baseUrl)) {
return locationHeaderValue.replace(baseUrl, "/api/.ory")
}

if (
locationHeaderValue.startsWith("/api/kratos/public/") ||
locationHeaderValue.startsWith("/self-service/") ||
locationHeaderValue.startsWith("/ui/")
) {
return "/api/.ory" + locationHeaderValue
}

return locationHeaderValue
}
12 changes: 12 additions & 0 deletions examples/nextjs-app-router/routers/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "@ory/elements-react/theme/styles.css"
import { PropsWithChildren } from "react"

export default async function AuthLayout({ children }: PropsWithChildren) {
return (
<>
<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen">
{children}
</main>
</>
)
}
62 changes: 62 additions & 0 deletions examples/nextjs-app-router/routers/app/login.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
redirectToBrowserEndpoint,
initOverrides,
onRedirect,
onValidationError,
QueryParams,
toFlowParams,
toValue,
} from "./utils"
import {LoginFlow, FlowType, handleFlowError, FrontendApi} from "@ory/client-fetch"

/**
* Use this method in an app router page to fetch an existing login flow or to create a new one. This method works with server-side rendering.
*
* ```
* import { getOrCreateLoginFlow } from "$/elements/frameworks/nextjs/routers/app/login"
* import { Login } from "$/elements/headless/flows/login"
*
* export default async function LoginPage({ searchParams }: PageProps) {
* const flow = await getOrCreateLoginFlow(searchParams)
*
* return (
* <Login
* flow={flow}
* config={{
* name: "My Project",
* sdk: { url: "https://<project.slug>.projects.oryapis.com" },
* }}
* />
* )
* }
* ```
*
* @param params The query parameters of the request.
* @param client The client to use for the request.
*/
export async function getOrCreateLoginFlow(
params: QueryParams,
client: FrontendApi
): Promise<LoginFlow | null> {
const onRestartFlow = () => redirectToBrowserEndpoint(params, FlowType.Login)
if (!params.flow) {
return onRestartFlow()
}

try {
const resp = await client.getLoginFlowRaw(
toFlowParams(params),
initOverrides,
)

return toValue(resp)
} catch (error) {
const errorHandler = handleFlowError({
onValidationError,
onRestartFlow,
onRedirect,
})
await errorHandler(error)
return null
}
}
Loading

0 comments on commit f34f04d

Please sign in to comment.