Next.js Quickstart
You will learn the following:
- Install
@clerk/nextjs
- Set your Clerk API keys
- Add
clerkMiddleware()
- Add
<ClerkProvider>
and Clerk components
Before you start
Example repositories
Install @clerk/nextjs
The Clerk Next.js SDK gives you access to prebuilt components, React hooks, and helpers to make user authentication easier.
Run the following command to install the SDK:
Get your API keys instantly by creating a free account
Sign up directly on this page to get your API keys and continue this quickstart guide without any interruptions.
Add the following keys to your .env.local
file. These keys can always be retrieved from the API Keys page of your Clerk Dashboard.
Add clerkMiddleware()
to your app
clerkMiddleware()
grants you access to user authentication state throughout your app, on any route or page. It also allows you to protect specific routes from unauthenticated users. To add clerkMiddleware()
to your app, follow these steps:
-
Create a
middleware.ts
file.- If you're using the
/src
directory, createmiddleware.ts
in the/src
directory. - If you're not using the
/src
directory, createmiddleware.ts
in the root directory alongside.env.local
.
- If you're using the
-
In your
middleware.ts
file, export theclerkMiddleware()
helper: -
By default,
clerkMiddleware()
will not protect any routes. All routes are public and you must opt-in to protection for routes. See theclerkMiddleware()
reference to learn how to require authentication for specific routes.
Add <ClerkProvider>
and Clerk components to your app
The <ClerkProvider>
component provides session and user context to Clerk's hooks and components. It's recommended to wrap your entire app at the entry point with <ClerkProvider>
to make authentication globally accessible. See the reference docs for other configuration options.
You can control which content signed-in and signed-out users can see with Clerk's prebuilt control components. Create a header using the following components:
<SignedIn>
: Children of this component can only be seen while signed in.<SignedOut>
: Children of this component can only be seen while signed out.<UserButton />
: Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.<SignInButton />
: An unstyled component that links to the sign-in page. In this example, since no props or environment variables are set for the sign-in URL, this component links to the Account Portal sign-in page.
Select your preferred router to learn how to make this data available across your entire app:
Create your first user
Run your project with the following command:
Visit your app's homepage at http://localhost:3000. Sign up to create your first user.
Create a custom sign-in or sign-up page
This tutorial gets you started with Clerk's <SignInButton />
component, which uses the Account Portal. If you don't want to use the Account Portal, read this guide about creating a custom authentication page.
Add custom onboarding to your authentication flow
If you need to collect additional information about users that Clerk's Account Portal or prebuilt components don't collect, read this guide about adding a custom onboarding flow to your authentication flow.
Protect specific routes
This tutorial taught you that by default, clerkMiddleware()
will not protect any routes. Read this reference doc to learn how to protect specific routes from unauthenticated users.
Read user and session data
Learn how to use Clerk's hooks and helpers to access the active session and user data in your Next.js app.
Next.js SDK Reference
Learn more about the Clerk Next.js SDK and how to use it.
Deploy to Production
Learn how to deploy your Clerk app to production.
Feedback
Last updated on