Docs
Authentication with Clerk
Authentication with Clerk
Learn how to integrate and configure Clerk for user authentication in your SaaS project.
Overview
This guide shows you how to set up user authentication using Clerk in your Next.js SaaS Starter. Clerk provides out-of-the-box sign-up, sign-in, account management, and more.
Prerequisites
- A Clerk account
- Environment variables added to your
.envfile
Refer to the Installation guide if you haven't set up your project yet.
1. Clerk Dashboard Setup
-
Sign in to your Clerk dashboard at clerk.com.
-
Create a new application and note down your credentials:
- Publishable Key (
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY) - Secret Key (
CLERK_SECRET_KEY)
- Publishable Key (
2. Environment Variables
Open your .env file (copied from .env.example) and add the following:
NEXT_PUBLIC_APP_URL=http://localhost:3000
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_XXXXXXXXXXXX
CLERK_SECRET_KEY=sk_test_XXXXXXXXXXXX These keys are sensitive. Do not commit them to version control. Use environment variables on your hosting platform.
3. Project Configuration
- Install Clerk in your project:
bun add @clerk/clerk-react(Or use npm / pnpm / yarn depending on your setup.)
4. Webhook Configuration
- Go to your Clerk dashboard and navigate to configuration > Webhooks.
- Add a new webhook with the following details:
- URL:
https://yourdomain.com/api/clerk/webhooks/user
- URL:
- Turn on the User Events
- User Created
- User Updated
- User Deleted
- Copy the Webhook Secret and add it to your
.envfile:
WEBHOOK_SECRET
The webhook secret is sensitive. Do not commit it to version control. Use environment variables on your hosting platform.
Next Steps
- Customize your sign-in, sign-up, and profile pages with
- Connect user roles or advanced logic in the Clerk dashboard
- Explore Clerk Docs for more advanced configuration.
app/
├── (auth)/ # Clerk authentication
│ ├── layout.tsx
│ └── (routes)/
│ ├── sign-in/
│ │ └── [[...sign-in]]/
│ │ └── page.tsx
│ └── sign-up/
│ └── -[[...sign-up]]/
│ └── -page.tsx
└── (server)/
└── api/
└── (routes)/
└── (webhooks)/ # webhook routes
└── user/
└── route.ts Your authentication system using Clerk is now ready. Continue exploring or set up additional features like social logins, multi-factor authentication, or user management in your Clerk dashboard.