feat(frontend): Refactor to cookie-based auth and add /api/v1 prefix

This commit is contained in:
Yunxiao Xu
2026-02-11 22:06:14 -08:00
parent 42f982e373
commit 7fe020f26c
8 changed files with 153 additions and 49 deletions

View File

@@ -1,6 +1,4 @@
import axios from "axios"
const API_URL = import.meta.env.VITE_API_URL || ""
import api from "./api"
export interface AuthResponse {
access_token: string
@@ -10,38 +8,40 @@ export interface AuthResponse {
export interface UserResponse {
id: string
email: string
display_name?: string
}
export const AuthService = {
async login(email: string, password: string): Promise<AuthResponse> {
const formData = new FormData()
formData.append("username", email) // OAuth2PasswordRequestForm uses 'username'
formData.append("username", email)
formData.append("password", password)
const response = await axios.post<AuthResponse>(`${API_URL}/auth/login`, formData)
if (response.data.access_token) {
localStorage.setItem("token", response.data.access_token)
}
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 axios.post<UserResponse>(`${API_URL}/auth/register`, {
const response = await api.post<UserResponse>("/auth/register", {
email,
password,
})
return response.data
},
logout() {
localStorage.removeItem("token")
async getMe(): Promise<UserResponse> {
const response = await api.get<UserResponse>("/auth/me")
return response.data
},
getToken() {
return localStorage.getItem("token")
},
isAuthenticated() {
return !!this.getToken()
async logout() {
await api.post("/auth/logout")
},
}