Spaces:
Sleeping
Sleeping
File size: 3,616 Bytes
194aaa5 8b922e3 194aaa5 5083221 194aaa5 5083221 194aaa5 5083221 194aaa5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | import type { UserInfo, LoginPayload, RegisterPayload, AuthResponse } from '../types';
const API_URL = import.meta.env.VITE_API_URL ?? '/api';
export function getAuthHeaders(): Record<string, string> {
const headers: Record<string, string> = {};
const token = localStorage.getItem('auth_token');
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const guestId = localStorage.getItem('guest_id');
if (guestId) {
headers['X-Guest-ID'] = guestId;
}
return headers;
}
export function setToken(token: string): void {
localStorage.setItem('auth_token', token);
}
export function clearToken(): void {
localStorage.removeItem('auth_token');
}
export function getToken(): string | null {
return localStorage.getItem('auth_token');
}
export async function login(payload: LoginPayload): Promise<AuthResponse> {
const formData = new URLSearchParams();
formData.append('username', payload.email);
formData.append('password', payload.password);
const res = await fetch(`${API_URL}/auth/jwt/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: formData,
});
if (!res.ok) {
const errData = await res.json().catch(() => ({}));
throw new Error(errData.detail || `Login failed (${res.status})`);
}
return res.json();
}
export async function register(payload: RegisterPayload): Promise<UserInfo> {
const res = await fetch(`${API_URL}/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
if (!res.ok) {
const errData = await res.json().catch(() => ({}));
throw new Error(errData.detail?.[0]?.msg || errData.detail || `Registration failed (${res.status})`);
}
return res.json();
}
export async function logout(): Promise<void> {
const res = await fetch(`${API_URL}/auth/jwt/logout`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders(),
},
});
if (!res.ok) {
const errData = await res.json().catch(() => ({}));
throw new Error(errData.detail || `Logout failed (${res.status})`);
}
}
export async function getMe(): Promise<UserInfo> {
const res = await fetch(`${API_URL}/users/me`, {
headers: {
'Content-Type': 'application/json',
...getAuthHeaders(),
},
});
if (!res.ok) {
throw new Error('Not authenticated');
}
return res.json();
}
export async function deleteAccount(): Promise<void> {
const res = await fetch(`${API_URL}/auth/me`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders(),
},
});
if (!res.ok) {
const errData = await res.json().catch(() => ({}));
throw new Error(errData.detail || 'Failed to delete account');
}
}
export async function claimGuestDocuments(guestId: string): Promise<void> {
const res = await fetch(`${API_URL}/auth/claim-guest-docs`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...getAuthHeaders(),
'X-Guest-ID': guestId,
},
});
if (!res.ok) {
const errData = await res.json().catch(() => ({}));
throw new Error(errData.detail || 'Failed to claim guest documents.');
}
}
export async function isAuthenticated(): Promise<boolean> {
const token = getToken();
if (!token) return false;
try {
await getMe();
return true;
} catch {
clearToken();
return false;
}
} |