Files
ccs/ui/src/lib/auth-api.ts
T
kaitranntt 464b410e8b feat(dashboard): add optional login authentication (#319)
Add session-based username/password auth for CCS dashboard:
- Backend: Express session middleware with bcrypt password verification
- Frontend: React AuthContext, login page, protected routes
- Config: dashboard_auth section in config.yaml + env var overrides
- Security: Rate limiting (5 attempts/15min), persistent session secret
- 16 unit tests for auth middleware

Auth disabled by default for backward compatibility.
2026-01-13 13:27:38 -05:00

62 lines
1.4 KiB
TypeScript

/**
* Auth API Client
* Handles authentication-related API calls.
*/
const BASE_URL = '/api/auth';
export interface AuthCheckResponse {
authRequired: boolean;
authenticated: boolean;
username: string | null;
}
export interface AuthSetupResponse {
enabled: boolean;
configured: boolean;
sessionTimeoutHours: number;
}
export interface LoginResponse {
success: boolean;
username: string;
}
async function request<T>(url: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${BASE_URL}${url}`, {
headers: { 'Content-Type': 'application/json' },
credentials: 'include', // Include cookies for session
...options,
});
if (!res.ok) {
const error = await res.json().catch(() => ({ error: 'Unknown error' }));
throw new Error(error.error || res.statusText);
}
return res.json();
}
/** Check authentication status */
export function checkAuth(): Promise<AuthCheckResponse> {
return request('/check');
}
/** Check auth setup status */
export function getAuthSetup(): Promise<AuthSetupResponse> {
return request('/setup');
}
/** Login with username/password */
export function login(username: string, password: string): Promise<LoginResponse> {
return request('/login', {
method: 'POST',
body: JSON.stringify({ username, password }),
});
}
/** Logout current session */
export function logout(): Promise<{ success: boolean }> {
return request('/logout', { method: 'POST' });
}