nanochat/services/frontend/lib/auth-client.ts
Manmohan Sharma aa7a907063
feat(frontend): wire frontend to real backend auth + chat-api services
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>
2026-04-16 13:21:38 -07:00

53 lines
1.3 KiB
TypeScript

const TOKEN_KEY = 'samosachaat_access_token';
const USER_KEY = 'samosachaat_user';
export interface TokenUser {
name: string;
email: string;
}
export function getToken(): string | null {
if (typeof window === 'undefined') return null;
return localStorage.getItem(TOKEN_KEY);
}
export function setToken(token: string): void {
localStorage.setItem(TOKEN_KEY, token);
// Decode JWT payload to persist basic user info
try {
const payload = JSON.parse(atob(token.split('.')[1]));
const user: TokenUser = {
name: payload.name || payload.email || 'User',
email: payload.email || '',
};
localStorage.setItem(USER_KEY, JSON.stringify(user));
} catch {
/* malformed JWT — ignore */
}
}
export function clearToken(): void {
localStorage.removeItem(TOKEN_KEY);
localStorage.removeItem(USER_KEY);
}
export function isAuthenticated(): boolean {
return !!getToken();
}
export function getUser(): TokenUser | null {
if (typeof window === 'undefined') return null;
try {
const raw = localStorage.getItem(USER_KEY);
return raw ? (JSON.parse(raw) as TokenUser) : null;
} catch {
return null;
}
}
export function authHeaders(): Record<string, string> {
const token = getToken();
if (!token) return {};
return { Authorization: `Bearer ${token}` };
}