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

Skip to main content

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.

info

The code used in the following quickstart is available in the App 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 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.

note

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.

  1. 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
    npm create next-app@latest
  2. Install the @ory/elements-react and @ory/nextjs package in your Next.js application.

    npm
    npm install @ory/elements-react @ory/nextjs
  3. 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 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
  4. 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 = {}

    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.

  5. Create a new folder named app in the root of your project. Inside the app folder, create a new folder named auth. Inside the auth folder, create the following files:

    • login/page.tsx
    • registration/page.tsx
    • recovery/page.tsx
    • verification/page.tsx

    Additionally, create a settings folder in the app directory:

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

    app/auth/login/page.tsx
    // Copyright © 2024 Ory Corp

    import { 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 (
    <Login
    flow={flow}
    config={config}
    components={{
    Card: {},
    }}
    />
    )
    }
  6. 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.

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