Next.js App Router Quickstart
This quickstart will guide you through the process of setting up Ory Elements with the App Router in a Next.js application.
The code used in the following quickstart is available in the App Router example GitHub repository
Prerequisites
Don't run Ory Tunnel and don't set the SDK URL to http://localhost:4000 for this quickstart. The @ory/nextjs middleware from
step 4 already serves the Ory APIs on your application's origin, in local development and in preview deployments. Guides that tell
you to start ory tunnel describe integrations that don't use @ory/nextjs.
This quickstart assumes you're using the alias configuration and not using a src/ directory. If your project uses src/, place
all files referenced as being in the root directory inside src/ instead.
-
Create a new Next.js application using your preferred package manager. You can use the following commands to create a new Next.js app:
- NPM
- PNPM
- Yarn
- Bun
npmnpm create next-app@latestpnpmpnpm create next-appyarnyarn create next-appbunbun create next-app -
Install the
@ory/elements-reactand@ory/nextjspackage in your Next.js application.- NPM
- PNPM
- Yarn
- Bun
npmnpm install @ory/elements-react @ory/nextjspnpmpnpm install @ory/elements-react @ory/nextjsyarnyarn add @ory/elements-react @ory/nextjsbunbun install @ory/elements-react @ory/nextjs -
Add the configuration for Ory Elements in your Next.js application. Create a new file named
ory.config.tsin the root of your project and add the following code:ory.config.ts// Copyright © 2024 Ory Corpimport type { OryClientConfiguration } from "@ory/elements-react"const config: OryClientConfiguration = {project: {default_redirect_url: "/",error_ui_url: "/auth/error",name: "Ory Next.js App Router Example",registration_enabled: true,verification_enabled: true,recovery_enabled: true,registration_ui_url: "/auth/registration",verification_ui_url: "/auth/verification",recovery_ui_url: "/auth/recovery",login_ui_url: "/auth/login",settings_ui_url: "/settings",},}export default config -
Add the middleware to your Next.js application. Create a new file named
middleware.tsin the root of your project and add the following code:middleware.ts// Copyright © 2024 Ory Corpimport { createOryMiddleware } from "@ory/nextjs/middleware"import oryConfig from "@/ory.config"// This function can be marked `async` if using `await` insideexport const middleware = createOryMiddleware({...oryConfig,// The origins this deployment serves. For any other origin, the app and Ory// use the Ory project's URL.trustedOrigins: ["http://localhost:3000"],})// See "Matching Paths" below to learn moreexport const config = {}The middleware serves Ory on your application's origin for local development and preview deployments. List those origins in
trustedOrigins, for example"http://localhost:3000"and a glob for previews such as"https://my-app-*-my-team.vercel.app". For any other origin, the app uses the Ory SDK URL and logs a warning. See Trusted origins.The middleware isn't supported in production. In production, use a custom domain on Ory Network, or your own domain with the Ory Enterprise License (OEL), on the same site as your application.
-
Create a new folder named
appin the root of your project. Inside theappfolder, create a new folder namedauth. Inside theauthfolder, create the following files:login/page.tsxregistration/page.tsxrecovery/page.tsxverification/page.tsx
Additionally, create a
settingsfolder in theappdirectory:settings/page.tsx
Each of these files will contain the respective Ory Elements component for the self-service flow. The flow functions attach the URL the server resolved, so the component reaches Ory at the same URL as the server. See Base URL.
- Login
- Registration
- Recovery
- Verification
- Settings
app/auth/login/page.tsx// Copyright © 2024 Ory Corpimport { Login } from "@ory/elements-react/theme"import { getLoginFlow, OryPageParams } from "@ory/nextjs/app"import config from "@/ory.config"export default async function LoginPage(props: OryPageParams) {const flow = await getLoginFlow(config, props.searchParams)if (!flow) {return null}return (<Loginflow={flow}config={config}components={{Card: {},}}/>)}app/auth/registration/page.tsx// Copyright © 2024 Ory Corpimport { Registration } from "@ory/elements-react/theme"import { getRegistrationFlow, OryPageParams } from "@ory/nextjs/app"import config from "@/ory.config"export default async function RegistrationPage(props: OryPageParams) {const flow = await getRegistrationFlow(config, props.searchParams)if (!flow) {return null}return (<Registrationflow={flow}config={config}components={{Card: {},}}/>)}app/auth/recovery/page.tsx// Copyright © 2024 Ory Corpimport { Recovery } from "@ory/elements-react/theme"import { getRecoveryFlow, OryPageParams } from "@ory/nextjs/app"import CustomCardHeader from "@/components/custom-card-header"import config from "@/ory.config"export default async function RecoveryPage(props: OryPageParams) {const flow = await getRecoveryFlow(config, props.searchParams)if (!flow) {return null}return (<Recoveryflow={flow}config={config}components={{Card: {Header: CustomCardHeader,},}}/>)}app/auth/verification/page.tsx// Copyright © 2024 Ory Corpimport CustomCardHeader from "@/components/custom-card-header"import { Verification } from "@ory/elements-react/theme"import { getVerificationFlow, OryPageParams } from "@ory/nextjs/app"import config from "@/ory.config"export default async function VerificationPage(props: OryPageParams) {const flow = await getVerificationFlow(config, props.searchParams)if (!flow) {return null}return (<Verificationflow={flow}config={config}components={{Card: {Header: CustomCardHeader,},}}/>)}app/settings/page.tsx// Copyright © 2024 Ory Corpimport { Settings } from "@ory/elements-react/theme"import { SessionProvider } from "@ory/elements-react/client"import { getOryBaseUrl, getSettingsFlow, OryPageParams } from "@ory/nextjs/app"import "@ory/elements-react/theme/styles.css"import config from "@/ory.config"export default async function SettingsPage(props: OryPageParams) {const flow = await getSettingsFlow(config, props.searchParams)if (!flow) {return null}return (<div className="flex flex-col gap-8 items-center mb-8"><SessionProvider baseUrl={await getOryBaseUrl()}><Settingsflow={flow}config={config}components={{Card: {},}}/></SessionProvider></div>)} -
Run the application and create your first user by navigating to
/auth/registration. After registration, you can log in at/auth/login. You can also access the other self-service flows at the following URLs:- Recovery:
/auth/recovery - Verification:
/auth/verification - Settings:
/settings
- NPM
- PNPM
- Yarn
- Bun
npmnpm run devpnpmpnpm devyarnyarn devbunbun devOpen your browser and navigate to
http://localhost:3000/auth/registrationto create your first user. - Recovery:
-
Optional: use your own Ory Network project by setting the
NEXT_PUBLIC_ORY_SDK_URLenvironment variable in your.envfile:.env# This is an example project, please replace it with yours!NEXT_PUBLIC_ORY_SDK_URL=https://nervous-lewin-4edwdlsbyw.projects.oryapis.com# Set the API Token for support of SAML and OIDC.ORY_PROJECT_API_TOKEN=You can find your Ory Network project URL in the Get started section of the Ory Console.
Next steps
Now that you have set up Ory Elements with the App Router in your Next.js application, you can further customize the components, add your own styles, and integrate them into your application as needed. You can also explore the Ory Elements documentation for more information on how to use the components and customize them to fit your needs. See Theming for more details on how to customize look and feel of the components.