Next.js Pages Router Quickstart
This quickstart will guide you through the process of setting up Ory Elements with the Pages Router in a Next.js application.
The code used in the following quickstart is available in the Pages 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 3 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.
-
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 { 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 = {} -
Create a new folder named
pagesin the root of your project. Inside thepagesfolder, create a new folder namedauth. Inside theauthfolder, create the following files:login.tsxregistration.tsxrecovery.tsxverification.tsx
Additionally, create a
settingsfolder in thepagesdirectory:settings.tsx
Each of these files will contain the respective Ory Elements component for the self-service flow.
- Login
- Registration
- Recovery
- Verification
- Settings
pages/auth/login.tsx// Copyright © 2024 Ory Corp"use client"import { Login } from "@ory/elements-react/theme"import "@ory/elements-react/theme/styles.css"import { useLoginFlow } from "@ory/nextjs/pages"import config from "@/ory.config"export default function LoginPage() {const flow = useLoginFlow()if (!flow) {return null}return (<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen"><Loginflow={flow}config={config}components={{Card: {},}}/></main>)}pages/auth/registration.tsx// Copyright © 2024 Ory Corp"use client"import { Registration } from "@ory/elements-react/theme"import "@ory/elements-react/theme/styles.css"import { useRegistrationFlow } from "@ory/nextjs/pages"import config from "@/ory.config"export default function RegistrationPage() {const flow = useRegistrationFlow()if (!flow) {return null}return (<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen"><Registrationflow={flow}config={config}components={{Card: {},}}/></main>)}pages/auth/recovery.tsx// Copyright © 2024 Ory Corp"use client"import { Recovery } from "@ory/elements-react/theme"import "@ory/elements-react/theme/styles.css"import { useRecoveryFlow } from "@ory/nextjs/pages"import config from "@/ory.config"export default function RecoveryPage() {const flow = useRecoveryFlow()if (!flow) {return null}return (<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen"><Recoveryflow={flow}config={config}components={{Card: {},}}/></main>)}pages/auth/verification.tsx// Copyright © 2024 Ory Corp"use client"import { Verification } from "@ory/elements-react/theme"import "@ory/elements-react/theme/styles.css"import { useVerificationFlow } from "@ory/nextjs/pages"import config from "@/ory.config"export default function VerificationPage() {const flow = useVerificationFlow()if (!flow) {return null}return (<main className="p-4 pb-8 flex items-center justify-center flex-col gap-8 min-h-screen"><Verificationflow={flow}config={config}components={{Card: {},}}/></main>)}settings/page.tsx// Copyright © 2024 Ory Corpimport config from "@/ory.config"import { Settings } from "@ory/elements-react/theme"import { useSettingsFlow } from "@ory/nextjs/pages"import { SessionProvider } from "../../../packages/elements-react/dist/client/session-provider"export default function SettingsPage() {const flow = useSettingsFlow()if (!flow) {return null}return (<div className="flex flex-col gap-8 items-center mb-8"><SessionProvider><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 Pages 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.