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
-
Install the
@ory/elements-react
and@ory/nextjs
package in your Next.js application.- NPM
- PNPM
- Yarn
- Bun
npm install @ory/elements-react @ory/nextjs
pnpm install @ory/elements-react @ory/nextjs
yarn install @ory/elements-react @ory/nextjs
bun install @ory/elements-react @ory/nextjs
-
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 -
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 -
Create a new folder named
pages
in the root of your project. Inside thepages
folder, create a new folder namedauth
. Inside theauth
folder, create the following files:login.tsx
registration.tsx
recovery.tsx
verification.tsx
Additionally, create a
settings
folder in thepages
directory: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.tsxpages/auth/registration.tsxpages/auth/recovery.tsxpages/auth/verification.tsxsettings/page.tsx -
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
npm run dev
pnpm dev
yarn dev
bun dev
Open your browser and navigate to
http://localhost:3000/auth/registration
to create your first user. - Recovery:
-
Optional: use your own Ory Network project by setting the
ORY_SDK_URL
environment variable in your.env
file: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 Component Custimization for more details on how to customize the components.