- Session-based auth — For browser users (OAuth, email/password)
- API key auth — For programmatic access (MCP endpoint)
Session-Based Authentication
Browser users authenticate via Supabase Auth, which manages sessions using secure HTTP-only cookies. Sessions are automatically refreshed by the Next.js middleware.Supported Methods
- GitHub OAuth
- Google OAuth
- Email + Password
Sign-In Flow
Authentication is handled by Server Actions insrc/app/auth/actions.ts.
GitHub OAuth
src/app/auth/actions.ts
Email + Password
src/app/auth/actions.ts
OAuth Callback
After OAuth providers redirect back to/auth/callback, the route handler exchanges the code for a session:
src/app/auth/callback/route.ts
Session Refresh (Middleware)
The middleware (src/lib/supabase/middleware.ts) automatically refreshes sessions on every request:
src/lib/supabase/middleware.ts
Checking Authentication
In Server Components and Server Actions, always verify the user before performing operations:Sign Out
src/app/auth/actions.ts
API Key Authentication
The MCP endpoint (/api/mcp) uses API key authentication instead of session cookies. This allows AI agents and scripts to access prompts programmatically.
API Key Structure
API keys are:- 64-character random strings generated with
crypto.randomBytes(32).toString('hex') - SHA-256 hashed before storage (plaintext is never stored)
- Scoped to a user (keys grant access to the owner’s prompts)
- Revocable (soft delete via
revoked_attimestamp)
Creating an API Key
Users create API keys from the/profile page. The Server Action (src/features/api-keys/actions.ts) generates the key, hashes it, and stores the hash:
src/features/api-keys/actions.ts
Verifying an API Key
The MCP route handler (src/app/api/mcp/route.ts) verifies API keys using the service-role client (which bypasses RLS):
src/lib/api-keys/verify.ts
Using API Keys
Clients pass API keys in one of two headers:Authorization: Bearer <key>(preferred)x-api-key: <key>(fallback)
src/app/api/mcp/route.ts
Example: Calling the MCP Endpoint
Revoking an API Key
src/features/api-keys/actions.ts
RLS and Authentication Context
Session-based auth: Supabase RLS policies useauth.uid() to identify the current user from the session cookie.
API key auth: The MCP endpoint uses the service-role client to bypass RLS, then manually filters data by user_id after verifying the API key.
supabase/migrations/20260208000001_prompt_schema.sql
Security Best Practices
- Never expose service-role key to client — Only import
createServiceClient()in server-side code - Always verify user identity — Check
auth.getUser()in every Server Action - Use RLS as defense-in-depth — Even if application logic fails, RLS enforces ownership
- Hash API keys — Store SHA-256 hashes, never plaintext
- Rotate keys regularly — Revoke and recreate API keys periodically
- Limit key count — Enforce a max of 10 active keys per user
Next Steps
- MCP Overview — Using the Model Context Protocol endpoint
- API Introduction — API architecture overview
- Database Schema — Understanding RLS policies