mirror of
https://github.com/karpathy/nanochat.git
synced 2026-05-13 11:20:21 +00:00
Remove NextAuth and replace with token-based auth against the backend
auth service (OAuth + JWT). The frontend now redirects login to
/api/auth/google and /api/auth/github (proxied by nginx to the auth
service), captures the JWT from the redirect query param, and uses it
for all API calls.
Key changes:
- Remove next-auth dependency and all NextAuth config/routes
- Add lib/auth-client.ts (JWT token storage + auth headers)
- Add hooks/useAuth.ts (client-side auth state + token capture)
- Rewrite middleware.ts to pass-through (client-side auth only)
- Login page uses plain <a> links to /api/auth/{provider}
- Chat page captures access_token from OAuth redirect
- Zustand store fetches conversations from real chat-api via JWT
- API routes proxy /api/conversations/* to chat-api with auth
- chat/stream route supports conversationId + auth header forwarding
- useSSE hook accepts auth headers for authenticated streaming
- Sidebar loads conversations from API, supports delete
- Landing page (Hero, LandingNav) uses useAuth instead of useSession
- Add .env.production.example and scripts/generate-jwt-keys.sh
Mock echo fallback preserved when CHAT_API_URL is not set.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
45 lines
1.5 KiB
TypeScript
45 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
export const runtime = 'nodejs';
|
|
|
|
const CHAT_API = process.env.CHAT_API_URL || 'http://chat-api:8002';
|
|
|
|
function getAuthHeader(req: NextRequest): string | null {
|
|
return req.headers.get('authorization');
|
|
}
|
|
|
|
export async function GET(req: NextRequest) {
|
|
const auth = getAuthHeader(req);
|
|
if (!auth) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
|
|
try {
|
|
const res = await fetch(`${CHAT_API}/api/conversations`, {
|
|
headers: { Authorization: auth, 'Content-Type': 'application/json' },
|
|
});
|
|
const data = await res.json();
|
|
return NextResponse.json(data, { status: res.status });
|
|
} catch (err) {
|
|
console.error('[conversations] proxy error:', err);
|
|
return NextResponse.json({ conversations: [] });
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const auth = getAuthHeader(req);
|
|
if (!auth) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
|
|
|
const body = await req.json();
|
|
try {
|
|
const res = await fetch(`${CHAT_API}/api/conversations`, {
|
|
method: 'POST',
|
|
headers: { Authorization: auth, 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
});
|
|
const data = await res.json();
|
|
return NextResponse.json(data, { status: res.status });
|
|
} catch (err) {
|
|
console.error('[conversations] create error:', err);
|
|
return NextResponse.json({ error: 'Failed to create conversation' }, { status: 500 });
|
|
}
|
|
}
|