Skip to content

Commit 12e3057

Browse files
docs: update nextjs quickstart (#2093)
* docs: update nexttjs example * docs: add .env.example * docs: add .env to nextjs example * docs: add protedted routes to nextjs example * docs: update nextjs quickstart * chore: format * chore: update readme and tunnel url * chore: format * docs: simplify next quickstart * docs: update nextjs readme * chore: make format * fix: missing SDK * chore: remove bg colour * chore: TODO section * chore: resolve feedback * fix: uniform ordering of headings in quickstarts * fix: add go to prod snippet --------- Co-authored-by: vinckr <[email protected]>
1 parent 5121763 commit 12e3057

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+6389
-5498
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NEXT_PUBLIC_ORY_SDK_URL=https://playground.projects.oryapis.com
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
NEXT_PUBLIC_ORY_SDK_URL=https://playground.projects.oryapis.com

code-examples/protect-page-login/nextjs/.eslintrc.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

code-examples/protect-page-login/nextjs/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/node_modules
55
/.pnp
66
.pnp.js
7+
.yarn/install-state.gz
78

89
# testing
910
/coverage
@@ -23,7 +24,6 @@
2324
npm-debug.log*
2425
yarn-debug.log*
2526
yarn-error.log*
26-
.pnpm-debug.log*
2727

2828
# local env files
2929
.env*.local
Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,4 @@
1-
This is a [Next.js](https://nextjs.org/) project bootstrapped with
2-
[`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
1+
This is a simple example application using Ory & Next.js app router.
32

4-
## Getting Started
5-
6-
First, run the development server:
7-
8-
```bash
9-
npm run dev
10-
# or
11-
yarn dev
12-
# or
13-
pnpm dev
14-
```
15-
16-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the
17-
result.
18-
19-
You can start editing the page by modifying `pages/index.tsx`. The page
20-
auto-updates as you edit the file.
21-
22-
[API routes](https://nextjs.org/docs/api-routes/introduction) can be accessed on
23-
[http://localhost:3000/api/hello](http://localhost:3000/api/hello). This
24-
endpoint can be edited in `pages/api/hello.ts`.
25-
26-
The `pages/api` directory is mapped to `/api/*`. Files in this directory are
27-
treated as [API routes](https://nextjs.org/docs/api-routes/introduction) instead
28-
of React pages.
29-
30-
This project uses
31-
[`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to
32-
automatically optimize and load Inter, a custom Google Font.
33-
34-
## Learn More
35-
36-
To learn more about Next.js, take a look at the following resources:
37-
38-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js
39-
features and API.
40-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
41-
42-
You can check out
43-
[the Next.js GitHub repository](https://github.com/vercel/next.js/) - your
44-
feedback and contributions are welcome!
45-
46-
## Deploy on Vercel
47-
48-
The easiest way to deploy your Next.js app is to use the
49-
[Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme)
50-
from the creators of Next.js.
51-
52-
Check out our
53-
[Next.js deployment documentation](https://nextjs.org/docs/deployment) for more
54-
details.
3+
To learn how to use this example, see
4+
[Next.js integration](https://www.ory.sh/docs/getting-started/integrate-auth/nextjs)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getServerSession } from "@ory/nextjs/app"
2+
import { NextResponse } from "next/server"
3+
4+
export async function GET() {
5+
// Check if the user is authenticated
6+
const session = await getServerSession()
7+
8+
if (!session) {
9+
// Return 401 if not authenticated
10+
return NextResponse.json({ message: "Unauthorized" }, { status: 401 })
11+
}
12+
13+
// Return data for authenticated users
14+
return NextResponse.json({
15+
message: "Hello from protected API!",
16+
user: session?.identity?.traits,
17+
})
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* Copyright © 2024 Ory Corp */
2+
/* SPDX-License-Identifier: Apache-2.0 */
3+
4+
@tailwind base;
5+
@tailwind components;
6+
@tailwind utilities;
7+
8+
body {
9+
color: var(--foreground);
10+
background: var(--background);
11+
font-family: Inter, Helvetica, sans-serif;
12+
}
13+
14+
@layer utilities {
15+
.text-balance {
16+
text-wrap: balance;
17+
}
18+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright © 2024 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import "./globals.css"
5+
import React, { Suspense, ReactNode } from "react"
6+
import { Inter } from "next/font/google"
7+
8+
const inter = Inter({ subsets: ["latin"] })
9+
10+
export default function RootLayout({
11+
children,
12+
}: Readonly<{
13+
children: ReactNode
14+
}>) {
15+
return (
16+
<html lang="en" suppressHydrationWarning className={inter.className}>
17+
<body>
18+
<Suspense>{children}</Suspense>
19+
</body>
20+
</html>
21+
)
22+
}
Lines changed: 8 additions & 0 deletions
Loading
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use client"
2+
3+
import { useEffect, useState } from "react"
4+
import { FrontendApi, Configuration, Session } from "@ory/client-fetch"
5+
6+
const basePath = process.env.NEXT_PUBLIC_ORY_SDK_URL || "http://localhost:4000"
7+
const ory = new FrontendApi(
8+
new Configuration({
9+
basePath,
10+
credentials: "include",
11+
}),
12+
)
13+
14+
export default function Page() {
15+
// highlight-start
16+
const [session, setSession] = useState<Session | null>(null)
17+
const [logoutUrl, setLogoutUrl] = useState<string | null>(null)
18+
19+
useEffect(() => {
20+
// Check if the user is authenticated
21+
const checkSession = async () => {
22+
try {
23+
const session = await ory.toSession()
24+
setSession(session)
25+
26+
// Get the logout URL once we have a session
27+
try {
28+
const { logout_url } = await ory.createBrowserLogoutFlow()
29+
setLogoutUrl(logout_url)
30+
} catch (logoutError) {
31+
console.error("Error creating logout flow:", logoutError)
32+
}
33+
} catch (error) {
34+
// No valid session found, redirect to Ory login
35+
window.location.href = `${basePath}/ui/login`
36+
}
37+
}
38+
39+
checkSession()
40+
}, [])
41+
// highlight-end
42+
43+
return (
44+
<main className="flex min-h-screen flex-col items-center justify-between p-24">
45+
<div className="w-full max-w-3xl">
46+
<div className="flex justify-between items-center mb-8">
47+
<h1 className="text-2xl font-bold">Welcome to Protected Page</h1>
48+
49+
{/* highlight-start */}
50+
{/* Logout button */}
51+
{logoutUrl && (
52+
<a
53+
href={logoutUrl}
54+
className="px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700 transition-colors"
55+
>
56+
Logout
57+
</a>
58+
)}
59+
{/* highlight-end */}
60+
</div>
61+
62+
<div className="p-4 rounded-lg overflow-auto">
63+
<h2 className="text-lg font-semibold mb-2">Session Information:</h2>
64+
<pre className="text-sm">{JSON.stringify(session, null, 2)}</pre>
65+
</div>
66+
</div>
67+
</main>
68+
)
69+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright © 2024 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
"use client"
5+
6+
export default function CustomCardHeader() {
7+
return <div>My custom card header</div>
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright © 2024 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { createOryMiddleware } from "@ory/nextjs/middleware"
5+
import oryConfig from "@/ory.config"
6+
7+
// This function can be marked `async` if using `await` inside
8+
export const middleware = createOryMiddleware(oryConfig)
9+
10+
// See "Matching Paths" below to learn more
11+
export const config = {}

code-examples/protect-page-login/nextjs/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/basic-features/typescript for more information.
5+
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.

code-examples/protect-page-login/nextjs/next.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type {import('next').NextConfig} */
2+
const nextConfig = {}
3+
4+
export default nextConfig
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright © 2024 Ory Corp
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import type { OryConfig } from "@ory/nextjs"
5+
6+
const config: OryConfig = {
7+
override: {
8+
applicationName: "Ory Next.js App Router Example",
9+
loginUiPath: "/auth/login",
10+
registrationUiPath: "/auth/registration",
11+
recoveryUiPath: "/auth/recovery",
12+
verificationUiPath: "/auth/verification",
13+
settingsUiPath: "/settings",
14+
defaultRedirectUri: "/",
15+
},
16+
}
17+
18+
export default config

0 commit comments

Comments
 (0)