We provide all the information about MCP servers via our MCP API.
curl -X GET 'https://glama.ai/api/mcp/v1/servers/bobmatnyc/mcp-memory-ts'
If you have feedback or need assistance with the MCP directory API, please join our Discord server
CLERK_MIGRATION.md•6.04 KiB
# Clerk Authentication Migration - Complete
This document summarizes the migration from NextAuth.js to Clerk authentication in the MCP Memory web interface.
## Migration Summary
Successfully replaced NextAuth.js with Clerk authentication v5.7.5. All authentication flows now use Clerk's managed authentication system.
## Changes Made
### 1. Dependencies Updated
**File: `web/package.json`**
- ❌ Removed: `next-auth@^4.24.0`
- ✅ Added: `@clerk/nextjs@^5.7.5`
### 2. Root Layout Updated
**File: `web/app/layout.tsx`**
- Replaced `<AuthProvider>` (NextAuth SessionProvider) with `<ClerkProvider>`
- Clerk provider now wraps the entire application
### 3. Middleware Updated
**File: `web/middleware.ts`**
- Replaced NextAuth middleware with Clerk's `clerkMiddleware`
- Updated to use Clerk v5 API with `createRouteMatcher`
- Public route: `/` (landing page)
- All other routes protected by Clerk authentication
### 4. Authentication Helper Updated
**File: `web/lib/auth.ts`**
- Replaced `getServerSession` and `authOptions` with Clerk's `auth()` and `currentUser()`
- Updated to use `@clerk/nextjs/server` imports (v5 API)
- Removed `requireAuth()` function (no longer needed)
- `getUserEmail()` now:
1. Gets userId from Clerk's `auth()`
2. Fetches user details with `currentUser()`
3. Extracts email from Clerk's user object
4. Ensures user exists in MCP Memory database
### 5. API Routes Updated
**Files: All API routes in `web/app/api/`**
- No direct changes needed
- All routes use `getUserEmail()` from `lib/auth.ts`
- Authentication automatically handled by updated `getUserEmail()` function
### 6. Navbar Component Updated
**File: `web/components/layout/navbar.tsx`**
- Replaced `useSession()` and `signOut()` with Clerk's `useUser()` and `<UserButton>`
- Removed custom sign out button
- Clerk's `<UserButton>` provides built-in profile/sign out UI
### 7. Landing Page Updated
**File: `web/app/page.tsx`**
- Replaced NextAuth redirect logic with Clerk's `auth()` check
- Added Clerk's `<SignInButton>` component for authentication
- Modal sign-in experience (no redirect to separate page)
### 8. Dashboard Page Updated
**File: `web/app/dashboard/page.tsx`**
- Replaced `requireAuth()` with direct `auth()` check
- Updated environment variable from `NEXTAUTH_URL` to `NEXT_PUBLIC_APP_URL`
### 9. Removed Files
**Deleted:**
- `web/app/api/auth/[...nextauth]/route.ts` (NextAuth API route)
- `web/app/auth/signin/page.tsx` (Custom sign-in page)
- `web/app/auth/error/page.tsx` (Custom error page)
### 10. Auth Provider Simplified
**File: `web/components/providers/auth-provider.tsx`**
- Removed NextAuth's `SessionProvider`
- Now a simple pass-through component (kept for backwards compatibility)
- ClerkProvider is used directly in root layout instead
### 11. Environment Configuration
**Created Files:**
- `web/.env.local` - Test environment with Clerk test keys
- `web/.env.production` - Production environment with Clerk live keys
- `web/.env.local.example` - Example configuration file
**Updated Files:**
- `web/.gitignore` - Added `.env.production` to ignore list
## Environment Variables
### Test Environment (.env.local)
```env
# Database
TURSO_URL=libsql://your-database.turso.io
TURSO_AUTH_TOKEN=your-auth-token
# OpenAI
OPENAI_API_KEY=your-openai-api-key
# Clerk Test
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_<your_publishable_key>
CLERK_SECRET_KEY=sk_test_<your_secret_key>
```
### Production Environment (.env.production)
```env
# Database
TURSO_URL=libsql://your-database.turso.io
TURSO_AUTH_TOKEN=your-auth-token
# OpenAI
OPENAI_API_KEY=your-openai-api-key
# Clerk Production
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_live_<your_publishable_key>
CLERK_SECRET_KEY=sk_live_<your_secret_key>
```
## Key Improvements
### 1. Simpler Authentication Flow
- No need to configure OAuth providers manually
- Clerk handles all authentication UI and flows
- Built-in user management dashboard
### 2. Better User Experience
- Modal sign-in (no page redirects)
- Professional user profile UI with `<UserButton>`
- Automatic session management
### 3. Enhanced Security
- Clerk manages session tokens and security
- Built-in protection against common attacks
- Regular security updates from Clerk
### 4. User Isolation
- Still maintains MCP Memory's user isolation by email
- `getUserEmail()` ensures user exists in database
- All API routes properly isolated per user
## Testing Checklist
After deployment, verify:
- [ ] Sign in with Clerk works (modal appears)
- [ ] User is redirected to dashboard after sign in
- [ ] Navbar shows user email and profile button
- [ ] API routes return data for authenticated user only
- [ ] Sign out works and redirects to landing page
- [ ] Different users see different memories (user isolation)
- [ ] Stats dashboard loads correctly
- [ ] Memory creation/search works
- [ ] Entity management works
## Known Issues
### Build Warnings (Pre-existing, not Clerk-related)
The following TypeScript errors exist in the parent project and are unrelated to the Clerk migration:
- Type errors in `src/core/memory-core.ts` (implicit any types)
- Type errors in `src/database/operations.ts` (implicit any types)
- Module resolution issues with `.js` extensions in imports
These should be fixed in the parent project's TypeScript configuration.
## Migration Commands
```bash
# 1. Install dependencies
cd web && npm install
# 2. Configure environment
cp .env.local.example .env.local
# Edit .env.local with your credentials
# 3. Run development server
npm run dev
# 4. Test authentication
# Visit http://localhost:3000
# Click "Sign In" and test Clerk authentication
```
## Rollback Instructions
If you need to rollback to NextAuth:
1. Restore `package.json` with `next-auth@^4.24.0`
2. Restore previous versions of files from git
3. Restore deleted auth pages and API route
4. Run `npm install`
5. Update `.env.local` with NextAuth variables
## Support
- **Clerk Documentation**: https://clerk.com/docs
- **Clerk Dashboard**: https://dashboard.clerk.com
- **MCP Memory Issues**: Check project GitHub issues
---
**Migration Date**: 2025-10-03
**Clerk Version**: 5.7.5
**Status**: ✅ Complete