Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 40x 40x 40x 3x 2x 1x 1x 1x 40x 13x 13x 11x 2x 2x 1x 1x 1x 1x 40x 12x | import { fetchApi } from '../core/fetchWrappers';
import { setTokens, clearTokens, getRefreshToken } from '../core/tokenHelpers';
export interface AuthResponse {
access: string;
refresh: string;
role: string;
email: string;
}
export interface LoginCredentials {
email: string;
password: string;
}
export async function login(credentials: LoginCredentials): Promise<AuthResponse> {
const data = await fetchApi<AuthResponse>('/auth/login/', {
method: 'POST',
body: JSON.stringify(credentials),
}, false);
if (!data.access) {
throw new Error('Token non trouvé dans la réponse du serveur.');
}
setTokens(data.access, data.refresh);
return data;
}
export async function refreshToken(): Promise<AuthResponse> {
const refresh = getRefreshToken();
if (!refresh) {
throw new Error("Refresh token manquant");
}
try {
const data = await fetchApi<AuthResponse>('/auth/refresh/', {
method: 'POST',
body: JSON.stringify({ refresh }),
}, false);
setTokens(data.access, data.refresh);
return data;
} catch {
clearTokens();
throw new Error("Refresh token expiré");
}
}
export function logout(): void {
clearTokens();
} |