Skip to main content

Next.js Quickstart

Cloud has the newest features. Some may not be in a release yet; the newest release is Identity & Access Management 7.3.0.

Use this guide to add Choreo Identity & Access Management authentication to a Next.js application using the @thunderid/nextjs SDK with full App Router support.

Get the complete sample

What You Will Learn​

  • Create a new Next.js app
  • Install the @thunderid/nextjs package
  • Add working sign-in and sign-out
  • Protect routes with middleware

Prerequisites​

  • About 15 minutes
  • Node.js installed on your system
  • npm, yarn, or pnpm
  • Your preferred code editor

Create an Application​

  1. Sign in to the Console at https://console.cloud.choreolabs.com/iam/. Select your project in the project switcher in the header.
  2. Navigate to Applications.
  3. Click Add application.
  4. From the Choose a type page, select Next.js.
  5. Enter a name (e.g. My Next.js App).
  6. Select how you want to sign in users (e.g., email/password, social login, etc.).
  7. Select Theme settings.
  8. Leave Sign-in approach set to Bring your own UI (the default for Next.js applications).
  9. Click Create.
Note

Copy the Application ID from the General tab, under Quick copy. Also copy the Flow secret from the window that pops up when the application is created; it's shown only once (if lost, regenerate it from the application's Credentials tab).

Data plane URL

Code in this guide uses https://<DATA_PLANE_URL> for your environment's data plane URL, where users sign in and tokens are issued. Replace it with the URL on the environment's Endpoints page. To open it, select your project, go to Deployment pipeline, and click Open environment on the environment's card. See Control Plane and Data Plane URLs.

Deploy your changes

Saving a change does not make it live. You author in the control plane, and your users sign in on a separate data plane, so a change takes effect in an environment only after you deploy it. Go to Deploy, deploy the latest version to your first environment, then promote it to the next environment when you are ready.

Auto-sync deploys saved changes to your first environment within seconds. Turning it on is your choice, per project. See Deploy a Version, or learn why you deploy and how auto-sync works.

Create a Next.js App​

Create your new Next.js app:

npx create-next-app@latest my-nextjs-app
cd my-nextjs-app

When prompted, select the App Router option (the default).

Install @thunderid/nextjs​

Install the Choreo Identity & Access Management Next.js SDK in your project:

npm install @thunderid/nextjs

Set Environment Variables​

Create a .env file in your project root with the following:

Configuration

Replace <your-application-id> and <your-flow-secret> with the values from your Choreo Identity & Access Management application.

Generate a random string for THUNDERID_SECRET (at least 32 characters):

openssl rand -base64 32
.env
NEXT_PUBLIC_THUNDERID_BASE_URL=https://<DATA_PLANE_URL>
NEXT_PUBLIC_THUNDERID_APPLICATION_ID=<your-application-id>
THUNDERID_FLOW_SECRET=<your-flow-secret>
NEXT_PUBLIC_THUNDERID_SIGN_IN_URL=/signin
NEXT_PUBLIC_THUNDERID_SIGN_UP_URL=/signup
THUNDERID_SECRET=<a-random-secret-for-session-signing>
# DANGER: Disables ALL TLS verification. Only for local development with self-signed certs. NEVER use in production.
NODE_TLS_REJECT_UNAUTHORIZED=0

Add ThunderIDProvider to Your Layout​

Wrap your root layout with ThunderIDProvider from the server export. This enables authentication across your entire app.

app/layout.tsx
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import { ThunderIDProvider } from '@thunderid/nextjs/server'
import "./globals.css";

const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});

const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html
lang="en"
className={`${geistSans.variable} ${geistMono.variable} h-full antialiased`}
>
<body className="min-h-full flex flex-col">
<ThunderIDProvider>{children}</ThunderIDProvider>
</body>
</html>
);
}

Add the Choreo Identity & Access Management Proxy​

Create a proxy.ts file at your project root to proxy requests through Choreo Identity & Access Management and protect routes:

proxy.ts
import {
thunderIDProxy,
createRouteMatcher,
} from '@thunderid/nextjs/server'

const isProtectedRoute = createRouteMatcher([
// Add the paths you want to protect, e.g. '/dashboard(.*)'
])

export default thunderIDProxy(async (thunderid, request) => {
if (isProtectedRoute(request)) {
await thunderid.protectRoute()
}
})

export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
}

Add Sign-In and Sign-Up Pages​

The <SignIn /> and <SignUp /> components render the sign-in and sign-up forms natively, using the Flow Execution API. Create the routes configured above as NEXT_PUBLIC_THUNDERID_SIGN_IN_URL and NEXT_PUBLIC_THUNDERID_SIGN_UP_URL:

app/signin/page.tsx
"use client";
import { useRouter } from "next/navigation";
import { SignIn } from "@thunderid/nextjs";

export default function SignInPage() {
const router = useRouter();

return (
<section className="flex flex-col items-center justify-center min-h-screen py-2">
<SignIn onSuccess={() => router.push("/")} />
</section>
);
}
app/signup/page.tsx
"use client";
import { SignUp } from "@thunderid/nextjs";

export default function SignUpPage() {
return (
<section className="flex flex-col items-center justify-center min-h-screen py-2">
<SignUp afterSignUpUrl="/" />
</section>
);
}

Build with Choreo Identity & Access Management components​

Update your home page with Choreo Identity & Access Management authentication components:

app/page.tsx
import { SignedIn, UserDropdown, SignedOut, SignInButton } from "@thunderid/nextjs";

export default function Home() {
return (
<section className="flex flex-col items-center justify-center min-h-screen py-2">
<SignedIn>
<UserDropdown />
</SignedIn>
<SignedOut>
<SignInButton>Sign In</SignInButton>
</SignedOut>
</section>
);
}
info

<SignInButton /> navigates to the local NEXT_PUBLIC_THUNDERID_SIGN_IN_URL route you created above, rather than redirecting to a Choreo Identity & Access Management-hosted page.

Run Your App​

Start the development server:

npm run dev

Visit your app at http://localhost:3000

Success

You should see the sign-in button. Click it, you'll land on your application's own /signin page. Authenticate with the test user you created in step 2, then return to your application with the user dropdown displayed.

Run the Sample App​

The Next.js sample is a working version of the app this guide builds. To run it instead of building the app yourself:

  1. Download the sample from its releases and open its folder.

  2. Create .env in the sample's folder with your application's values:

    .env
    NEXT_PUBLIC_THUNDERID_BASE_URL=https://<DATA_PLANE_URL>
    NEXT_PUBLIC_THUNDERID_APPLICATION_ID=<your-application-id>
    THUNDERID_FLOW_SECRET=<your-flow-secret>
    THUNDERID_SECRET=<a-random-secret-for-session-signing>
    NEXT_PUBLIC_THUNDERID_SIGN_IN_URL=/signin
    NEXT_PUBLIC_THUNDERID_SIGN_UP_URL=/signup
  3. Install the dependencies and start the app:

    npm install
    npm run dev
  4. Open http://localhost:3000 and sign in.

What's Next​

Was this page helpful?