/
FlysAt
/
DataFoundry
Обзор
Документация
Войти
/
FlysAt
/
DataFoundry
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
frontend/src/api/auth.ts
110 строк
3 KB
FlysAt8
Интеграция фронта
01 апр 2026, 14:01
01 апр 2026, 14:01
539a004
Код
Авторство
О чём код?
// src/api/auth.ts import { API_BASE_URL } from './config'; export interface RegisterData { email: string; password: string; full_name?: string; // используем undefined, а не null roles?: string[]; } export interface LoginData { email: string; password: string; } export interface AuthResponse { access_token: string; refresh_token: string; token_type: string; } export interface User { id: number; email: string; full_name?: string; roles: string[]; } export const authAPI = { register: async (userData: RegisterData): Promise<User> => { const response = await fetch(`${API_BASE_URL}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: userData.email, password: userData.password, full_name: userData.full_name, roles: userData.roles || ['customer', 'executor'], // по умолчанию обе роли }), }); if (!response.ok) { const error = await response.json(); const customError = new Error(error.detail || 'Registration failed'); (customError as any).status = response.status; (customError as any).details = error; throw customError; } return response.json(); }, login: async (credentials: LoginData): Promise<AuthResponse> => { const response = await fetch(`${API_BASE_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email: credentials.email, password: credentials.password, }), }); if (!response.ok) { const error = await response.json(); const customError = new Error(error.detail || 'Login failed'); (customError as any).status = response.status; // Сохраняем статус throw customError; } return response.json(); }, getMe: async (token: string): Promise<User> => { const response = await fetch(`${API_BASE_URL}/auth/me`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${token}`, }, }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Failed to get user data'); } return response.json(); }, refresh: async (refreshToken: string): Promise<AuthResponse> => { const response = await fetch(`${API_BASE_URL}/auth/refresh`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ refresh_token: refreshToken }), }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Token refresh failed'); } return response.json(); }, };