fix(auth): Correct API paths and resolve build/type errors in tests and config
This commit is contained in:
47
frontend/src/services/auth.ts
Normal file
47
frontend/src/services/auth.ts
Normal 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()
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user