New Ory Agent Security is now live! Claim your complimentary test drive. Get Started!

Skip to main content

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.

info

The code used in the following quickstart is available in the Pages Router example GitHub repository

Prerequisites​

warning

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.

  1. Install the @ory/elements-react and @ory/nextjs package in your Next.js application.

    npm
    npm install @ory/elements-react @ory/nextjs
  2. Add the configuration for Ory Elements in your Next.js application. Create a new file named ory.config.ts in the root of your project and add the following code:

    ory.config.ts
    // Copyright © 2024 Ory Corp

    import { 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
  3. Add the middleware to your Next.js application. Create a new file named middleware.ts in the root of your project and add the following code:

    middleware.ts
    // Copyright © 2024 Ory Corp

    import { createOryMiddleware } from "@ory/nextjs/middleware"
    import oryConfig from "./ory.config"

    // This function can be marked `async` if using `await` inside
    export 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 more
    export const config = {}
  4. Create a new folder named pages in the root of your project. Inside the pages folder, create a new folder named auth. Inside the auth folder, create the following files:

    • login.tsx
    • registration.tsx
    • recovery.tsx
    • verification.tsx

    Additionally, create a settings folder in the pages directory:

    • settings.tsx

    Each of these files will contain the respective Ory Elements component for the self-service flow.

    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">
    <Login
    flow={flow}
    config={config}
    components={{
    Card: {},
    }}
    />
    </main>
    )
    }
  5. 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
    npm run dev

    Open your browser and navigate to http://localhost:3000/auth/registration to create your first user.

  6. Optional: use your own Ory Network project by setting the NEXT_PUBLIC_ORY_SDK_URL environment variable in your .env file:

    .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.