Clerk Authentication Setup
Authentication System: LogoAIpro uses Clerk for secure user authentication. Clerk provides email/password, social logins, and complete user management out of the box.
Why Clerk?
Clerk is a complete authentication solution that provides:
- Multiple Sign-In Methods: Email/password, Google OAuth, and more
- User Management: Complete user profiles, avatars, and session management
- Security: Built-in security features, password hashing, and session management
- Easy Integration: Simple Next.js integration with
@clerk/nextjs - Webhooks: Real-time user events for database synchronization
Step-by-Step Setup
Step 1: Create Clerk Account
- Go to clerk.com
- Click "Start for Free" or "Sign Up"
- Sign up with your email or GitHub account
- Verify your email address
- Complete the onboarding process
Step 2: Create a New Application
- After signing in, click "Create Application"
- Enter application name: LogoAIpro
- Choose authentication options:
- Email address
- Password
- Google (recommended)
- Click "Create Application"
Step 3: Configure Authentication Settings
- Navigate to Settings → User & Authentication
- Configure sign-in methods:
- Enable "Email address" and "Password"
- Enable "Google" if you want social login
- Set password requirements (recommended: at least 8 characters)
- Configure email verification (recommended: enable)
Step 4: Get API Keys
- Navigate to API Keys in the sidebar
- You'll see two keys:
- Publishable Key: Starts with
pk_test_orpk_live_ - Secret Key: Starts with
sk_test_orsk_live_
- Publishable Key: Starts with
- Copy both keys (you'll need them for your
.env.localfile)
# Add to .env.local
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_xxxxxxxxxxxxx
CLERK_SECRET_KEY=sk_test_xxxxxxxxxxxxxStep 5: Configure Application URLs
- Navigate to Settings → Paths
- Set up your application URLs:
# Development Sign-in URL: http://localhost:3000/login Sign-up URL: http://localhost:3000/sign-up After sign-in: http://localhost:3000/dashboard After sign-up: http://localhost:3000/dashboard # Production (after deployment) Sign-in URL: https://yourdomain.com/login Sign-up URL: https://yourdomain.com/sign-up After sign-in: https://yourdomain.com/dashboard After sign-up: https://yourdomain.com/dashboard - Save your changes
Step 6: Set Up Webhooks (Required)
Webhooks are essential for syncing user data to your MongoDB database.
- Navigate to Webhooks in the sidebar
- Click "Add Endpoint"
- Configure the webhook:
- Endpoint URL:
# Development (use ngrok for local testing) https://your-ngrok-url.ngrok.io/api/webhooks/clerk # Production https://yourdomain.com/api/webhooks/clerk - Subscriptions: Select these events:
-
user.created- When a user signs up -
user.updated- When user data changes -
user.deleted- When a user is deleted
-
- Endpoint URL:
- Click "Create"
- Copy the Signing Secret (starts with
whsec_) - Add it to your
.env.local:CLERK_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxx
Webhook Testing (Local Development):
- Use ngrok to expose your local server
- Run:
ngrok http 3000 - Use the ngrok URL as your webhook endpoint
- Update the webhook URL in Clerk dashboard
Step 7: Configure Next.js Integration
LogoAIpro already has Clerk configured. Verify your middleware.ts file:
import { clerkMiddleware, createRouteMatcher } from '@clerk/nextjs/server'
const isPublicRoute = createRouteMatcher([
'/',
'/login(.*)',
'/sign-up(.*)',
'/api/webhooks(.*)',
])
export default clerkMiddleware((auth, request) => {
if (!isPublicRoute(request)) {
auth().protect()
}
})
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)(.*)',
],
}Step 8: Test Authentication
- Start your development server:
npm run dev - Navigate to
http://localhost:3000 - Click "Sign Up" or "Get Started"
- Try creating an account with email/password
- Verify you're redirected to the dashboard
- Check your MongoDB database for the new user entry
Production Setup
Switching to Production Keys
- In Clerk Dashboard, toggle from "Test" to "Production" mode
- Get your production API keys
- Update environment variables on your production server:
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_live_xxxxxxxxxxxxx CLERK_SECRET_KEY=sk_live_xxxxxxxxxxxxx CLERK_WEBHOOK_SECRET=whsec_xxxxxxxxxxxxx - Update webhook URL to your production domain
- Test authentication in production
Advanced Configuration
Social Logins (Google OAuth)
- In Clerk Dashboard, go to Settings → Social Connections
- Click on "Google"
- Enable Google authentication
- Configure Google OAuth (see Google OAuth setup guide)
- Save changes
Custom User Metadata
LogoAIpro stores additional user data in MongoDB. The webhook handler at app/api/webhooks/clerk/route.ts automatically syncs user data.
Verification Checklist
Ensure you have:
- Clerk account created
- Application created in Clerk
- API keys added to
.env.local - Webhook configured and secret added
- Application URLs configured
- Test sign-up/sign-in working
- User data syncing to MongoDB
Next Steps: After Clerk is configured: