Files
ea-chatbot-lg/frontend/src/services/auth.ts
2026-02-18 13:43:37 -08:00

65 lines
1.7 KiB
TypeScript

import api from "./api"
import type { Theme } from "@/components/theme-provider"
export interface AuthResponse {
access_token: string
token_type: string
}
export interface UserResponse {
id: string
email: string
display_name?: string
theme_preference: Theme
}
export const AuthService = {
async login(email: string, password: string): Promise<AuthResponse> {
const formData = new FormData()
formData.append("username", email)
formData.append("password", password)
const response = await api.post<AuthResponse>("/auth/login", formData)
return response.data
},
async loginWithOIDC() {
const response = await api.get<{ url: string }>("/auth/oidc/login")
if (response.data.url) {
window.location.href = response.data.url
}
},
async register(email: string, password: string): Promise<UserResponse> {
const response = await api.post<UserResponse>("/auth/register", {
email,
password,
})
return response.data
},
async getMe(): Promise<UserResponse> {
const response = await api.get<UserResponse>("/auth/me")
// Double check that we got JSON and not an HTML fallback
const contentType = response.headers["content-type"]
if (contentType && !contentType.includes("application/json")) {
throw new Error("Invalid response from server")
}
return response.data
},
async logout() {
await api.post("/auth/logout")
},
async refreshSession(): Promise<AuthResponse> {
const response = await api.post<AuthResponse>("/auth/refresh")
return response.data
},
async updateTheme(theme: Theme): Promise<UserResponse> {
const response = await api.patch<UserResponse>("/auth/theme", { theme })
return response.data
},
}