Overview
PDF AI uses Clerk for user authentication, providing secure sign-in, sign-up, and session management. Clerk handles all authentication flows, user management, and protects routes from unauthorized access.Configuration
Environment Variables
Add your Clerk credentials to.env:
Get your API keys from the Clerk Dashboard
Sign-In/Sign-Up URLs
Configure authentication page routes:Middleware Protection
Clerk’s middleware protects all routes except public ones (src/middleware.ts:1-13):
Public Routes
The following routes are accessible without authentication:/- Home page/api/webhook- Webhook endpoints (for Stripe integration)
All other routes require authentication. Unauthenticated users are automatically redirected to the sign-in page.
Usage in Components
Server Components
Use theauth() helper to get the current user’s session (src/app/page.tsx:15-24):
User Button Component
Clerk provides a pre-built user profile button (src/app/page.tsx:32):
The
UserButton displays the user’s avatar and provides access to account settings and sign-out functionality.Conditional Rendering
Show different UI based on authentication status (src/app/page.tsx:35-60):
API Routes
Protect API routes using Clerk’s authentication:User Identification
Clerk provides a uniqueuserId for each authenticated user:
userId is used to:
- Associate chats with users in the database
- Check subscription status
- Filter user-specific data
Webhook Integration
Clerk can send webhooks for user lifecycle events:Session Management
Clerk automatically handles:- Session creation on sign-in
- Session refresh tokens
- Secure cookie management
- Session expiration and renewal
Sign-In Flow
- User clicks “Login to get Started!” button
- Redirected to
/sign-inpage - Clerk handles authentication (email, OAuth, etc.)
- After successful sign-in, user is redirected to
/ - Session is established and
userIdis available
Components Reference
ClerkProvider
Wrap your app withClerkProvider in app/layout.tsx:
SignIn Component
Create a sign-in page atapp/sign-in/[[...sign-in]]/page.tsx:
SignUp Component
Create a sign-up page atapp/sign-up/[[...sign-up]]/page.tsx:
Dependencies
Best Practices
- Server-Side Auth: Use
auth()in Server Components for secure authentication checks - Middleware Protection: Apply
authMiddlewareto protect all routes by default - Public Routes: Explicitly list public routes to avoid blocking legitimate access
- Type Safety: Use TypeScript to ensure
userIdnull checks
Customization
Custom Sign-In Experience
Customize Clerk’s appearance to match your brand:Additional User Data
Access more user information:Troubleshooting
Common Issues
Redirect Loops- Ensure
/sign-inand/sign-upare inpublicRoutesif you have custom pages - Check that environment variables are correctly set
- Verify
CLERK_SECRET_KEYis set in your environment - Clear browser cookies and try again
- Confirm the request includes authentication cookies
- Check that the route is not in
publicRoutesif it should be protected
- Ensure
/api/webhookis inpublicRoutes - Verify webhook URL is correctly configured in Clerk Dashboard
- Implement signature verification for security