Prerequisites
- A Clerk account (sign up at clerk.com)
- A Clerk application created for your Typeset instance
Creating a Clerk application
- Sign in to your Clerk dashboard
- Click Create application
- Choose a name for your application (e.g., “Typeset Self-Hosted”)
- Select the authentication methods you want to enable:
- Email and password
- Social sign-in (Google, GitHub, etc.)
- Phone number
- Click Create application
Obtaining API keys
After creating your application:- Navigate to API Keys in the sidebar
- Copy your Publishable key and Secret key
- Add them to your
.env.localfile:
How authentication works
Typeset’s authentication flow is implemented using Clerk’s Next.js SDK:Application wrapper
The root layout wraps the entire application withClerkProvider:
app/layout.tsx
app/layout.tsx:32.
Protected routes
Typeset uses middleware to protect specific routes that require authentication:middleware.ts
middleware.ts:3-12 for the complete implementation.
Route protection behavior
The middleware is configured to only run on specific routes:/my-projects- User’s project list/shared-with-me- Projects shared with the user/project/*- Individual project pages/api/*- All API routes
/, /home, /privacy, /terms) are not matched by the middleware, so they load instantly without any Clerk initialization.
This selective middleware approach ensures fast loading times for public pages while protecting authenticated routes.
Integration with Liveblocks
Clerk authentication is used to identify users for Liveblocks collaboration:app/api/liveblocks-auth/route.ts
app/api/liveblocks-auth/route.ts:27-50 for the full implementation.
Configuring authentication methods
In your Clerk dashboard, you can customize:Email and password
- Go to User & Authentication > Email, Phone, Username
- Enable Email address
- Configure password requirements
Social sign-in
- Go to User & Authentication > Social Connections
- Enable providers (Google, GitHub, Microsoft, etc.)
- Follow provider-specific setup instructions
Multi-factor authentication
- Go to User & Authentication > Multi-factor
- Enable SMS or authenticator app MFA
- Configure whether MFA is optional or required
Customizing the user interface
Typeset uses Clerk’s pre-built components. You can customize the appearance:- Go to Customization > Theme in your Clerk dashboard
- Adjust colors, fonts, and layout to match your branding
- Use the dark theme option to match Typeset’s design
Typeset includes
@clerk/themes package which provides pre-built theme options. You can apply these in the ClerkProvider component.User profile management
Clerk automatically provides user profile management pages. Users can:- Update their profile information
- Change their password
- Manage connected accounts
- Configure MFA settings
- View active sessions
Testing authentication
After configuration:- Start your development server:
npm run dev - Navigate to
/my-projects- you should be redirected to sign in - Create a test account or sign in
- Verify you can access protected routes
- Check that user information displays correctly
Production considerations
Allowed redirect URLs
In your Clerk dashboard under Paths:- Add your production domain to Authorized redirect URLs
- Add sign-in and sign-up redirect paths:
https://yourdomain.com/my-projectshttps://yourdomain.com/project/*
Session management
Configure session lifetime in Sessions settings:- Session duration: How long users stay signed in
- Inactivity timeout: Auto sign-out after inactivity
- Multi-session handling: Allow multiple simultaneous sessions
Troubleshooting
Users not redirecting after sign-in
Check that your redirect URLs are properly configured in Clerk dashboard and match your application routes.API key errors
Ensure your.env.local keys match the environment (development/production) you’re running in.
CORS errors
Verify your domain is added to allowed origins in Clerk dashboard under API Keys > CORS.Next steps
Database configuration
Set up Liveblocks for real-time collaboration
Environment variables
Review all required configuration variables