samar m
feat: implement apiFetch wrapper and auth API calls
09a5796
import { apiFetch } from './client'
import type { AuthResponse } from '../types'
export async function login(email: string, password: string): Promise<AuthResponse> {
const res = await apiFetch('/api/auth/login', {
method: 'POST',
body: JSON.stringify({ email, password }),
})
if (!res.ok) {
const err = await res.json()
throw new Error(err.detail ?? 'Login failed')
}
return res.json()
}
export async function signup(
full_name: string,
email: string,
password: string,
role: string,
class_code?: string
): Promise<AuthResponse> {
const res = await apiFetch('/api/auth/signup', {
method: 'POST',
body: JSON.stringify({ full_name, email, password, role, class_code }),
})
if (!res.ok) {
const err = await res.json()
throw new Error(err.detail ?? 'Signup failed')
}
return res.json()
}
export async function logout(): Promise<void> {
await apiFetch('/api/auth/logout', { method: 'POST' })
}
export async function refreshToken(): Promise<{ access_token: string }> {
const res = await fetch('/api/auth/refresh', {
method: 'POST',
credentials: 'include',
})
if (!res.ok) throw new Error('Refresh failed')
return res.json()
}