This is a complete starter template for building authentication-powered applications using Next.js, Tailwind CSS, Clerk and MongoDB with Mongoose for database management. It comes pre-configured with user authentication, database integration, and a customizable UI to help you get started quickly.
Clone the repository git clone https://github.com/ParnaRoyChowdhury777/next-tailwind-clerk-mongoose-mongodb.git
or download the zip file.
Open the project in your favorite code editor.
Navigate to the project directory cd next-tailwind-clerk-mongoose-mongodb.
Install the dependencies bun install
.
Create a .env file in the root directory which contains the following:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_YOUR_CLERK_PUBLISHABLE_KEY
CLERK_SECRET_KEY=sk_test_YOUR_CLERK_SECRET_KEY
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
MONGODB_URI=<Your MongoDb URI>
WEBHOOK_SECRET=<Your Clerk Webhook Secret>
Start the development server bun run dev
Open http://localhost:3000 in your browser
Run the following command to bootstrap a Next.js app with the required configuration:
bun create next-app <app_name>
When prompted:
src/
directory: Yes@/*
PUBLISHABLE_KEY
and SECRET_KEY
.Run the following command to install the Clerk SDK:
bun install @clerk/nextjs
Add the following keys to your .env.local
file:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_YOUR_CLERK_PUBLISHABLE_KEY
CLERK_SECRET_KEY=sk_test_YOUR_CLERK_SECRET_KEY
middleware.ts
FileCreate a middleware.ts
file in the src/
directory and add:
import { clerkMiddleware } from "@clerk/nextjs/server";
export default clerkMiddleware();
export const config = {
matcher: [
'/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
'/(api|trpc)(.*)',
],
};
ClerkProvider
In the src/app/layout.tsx
file, wrap your application with the ClerkProvider
:
import { ClerkProvider, SignedIn, SignedOut, SignInButton, UserButton } from "@clerk/nextjs";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<ClerkProvider>
<html lang="en">
<body>
<SignedOut>
<SignInButton />
</SignedOut>
<SignedIn>
<UserButton />
</SignedIn>
{children}
</body>
</html>
</ClerkProvider>
);
}
You can create custom pages for sign-in and sign-up by adding the following files in the src/app
directory:
sign-in/[[...sign-in]]/page.tsx
sign-up/[[...sign-up]]/page.tsx
Add your custom logic and UI components to these files as needed.
Run the following command to install MongoDB and Mongoose:
bun install mongodb mongoose svix
Create a src/db.ts
file and set up your MongoDB connection
Create a src/models/user.model.ts
file for a basic User schema
Create a src/actions/user.action.ts
file for basic User actions
Start your application with:
bun run dev
Your app is now running at http://localhost:3000
.
.
├── src
│ ├── actions
│ │ └── user.action.ts
│ ├── app
│ │ ├── api
│ │ ├── layout.tsx // App layout with ClerkProvider
│ │ ├── page.tsx // Default home page
│ │ ├── sign-in // Custom sign-in page
│ │ ├── sign-up // Custom sign-up page
│ │ └── globals.css
│ ├── db.ts
│ ├── middleware.ts // Clerk middleware
│ ├── models
│ │ └── user.model.ts // Example Mongoose schema
├── .env.local // Environment variables
└── README.md // Documentation
Feel free to contribute to this project by submitting issues or pull requests. Let's make this starter kit even better!