Welcome to my step-by-step guide on adding authentication to your Next.js application using Clerk! If you're like me, you understand the importance of user authentication in web development but also know the headache of implementing it. That's where the Clerk comes in.
Clerk is a fantastic tool that simplifies adding authentication to your Next.js projects. Whether you're building a simple blog, an e-commerce site, or a complex web application, Clerk has got you covered.
In this guide, I'll walk you through the process of setting up authentication in your Next.js app using Clerk, allowing your users to sign up, log in, and access protected routes with ease. So let's dive in and get started!
Step 1: Creating a Clerk Application
- Login to your Clerk dashboard and create a new application. Enter your app name, enable Email and Google options and click Create application.
- Copy the environment variables from the Clerk dashboard.
Step 2: Setting Up Your Next.js Project
First things first, make sure you have a NextJs project up and running. If you haven't already set one up, you can easily create a new NextJs app using the following command:
npx create-next-app my-next-app
Replace my-next-app with your preferred project name. select typescript and select yes for the src folder. Once the project is created, navigate into the directory:
cd my-next-app
Step 3: Installing Clerk
Now, let's install Clerk in our Next.js project. Clerk provides a hassle-free way to handle authentication, so you can focus on building your app. Just run this command to get started:
npm install @clerk/nextjs
Step 4: Initial Setup
The above is the structure of the application.
Include the following environment variables in the .env file:
Step 5: Adding ClerkProvider to the application
ClerkProvider is a component provided by the Clerk authentication library for NextJs, which wraps the entire application and provides authentication context to its children.
In the layout.tsx file of the app directory, import the ClerkProvider from @clerk/nextjs.
Step 6: Adding Authentication
Now that Clerk is installed and integrated into your application, you have the flexibility to determine which pages remain accessible to the public and which require authentication for access. Create a middleware.ts file within the src/ directory.
The authMiddleware() function facilitates authentication processes and restricts entry to routes for visitors who are not logged in. In Next.js, middleware functions operate exclusively on routes designated with matcher().
Step 7: adding sign-in and signup page
The clerk has prebuilt components that we can use to handle the signing and signup easily. In the page.tsx of signin folder add the following code.
The SignIn component is a prebuilt component that is used to handle the signin inside our app. In the page.tsx of the signup folder add the following code.
Step 8: Adding the signout Option
In page.tsx of the app directory add the following code.
The auth() function retrieves the userId of the user if they are logged in. Meanwhile, the currentUser() function fetches user details such as their first name and last name. Additionally, the SignOutButton component manages the signout functionality.
You can find the source code here.