fix(auth): Correct API paths and resolve build/type errors in tests and config

This commit is contained in:
Yunxiao Xu
2026-02-11 20:30:32 -08:00
parent e3bc699c64
commit f8612cfcb8
5 changed files with 109 additions and 6 deletions

View File

@@ -0,0 +1,47 @@
import axios from "axios"
const API_URL = import.meta.env.VITE_API_URL || ""
export interface AuthResponse {
access_token: string
token_type: string
}
export interface UserResponse {
id: string
email: 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("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)
}
return response.data
},
async register(email: string, password: string): Promise<UserResponse> {
const response = await axios.post<UserResponse>(`${API_URL}/auth/register`, {
email,
password,
})
return response.data
},
logout() {
localStorage.removeItem("token")
},
getToken() {
return localStorage.getItem("token")
},
isAuthenticated() {
return !!this.getToken()
},
}