/
viktorphp
/
front-socket
Обзор
Документация
Войти
/
viktorphp
/
front-socket
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/features/auth/api/authService.ts
58 строк
2 KB
Viktorphp84
feature-socket: test.
09 окт 2025, 10:41
09 окт 2025, 10:41
2003245
Код
Авторство
О чём код?
import { z } from 'zod'; import { get, post } from '@shared/api/rest'; import { IUser } from '@shared/types'; export const LoginRequestSchema = z.object({ login: z.string().optional(), username: z.string().optional(), email: z.string().optional(), password: z.string().optional(), }); export type ILoginRequest = z.infer<typeof LoginRequestSchema>; export const authServices = { loginSession: async (body: ILoginRequest) => { const result = LoginRequestSchema.safeParse(body); if (!result.success) { throw new Error(`Ошибка валидации: ${result.error.message}`); } return post<{ status: number; message: string; user: IUser }>( '/auth/loginSession', body ); }, checkSession: async (options?: { headers?: Record<string, string>; }): Promise<IUser> => { const response = await get('/profile/users/checkSession', { headers: options?.headers, }); return response.data; }, register: async (body: ILoginRequest) => { const result = LoginRequestSchema.safeParse(body); if (!result.success) { throw new Error(`Ошибка валидации: ${result.error.message}`); } return post<{ status: number; message: string; user: IUser }>( '/auth/register', body ); }, logout: async () => { document.cookie.split(';').forEach((cookie) => { const [name] = cookie.split('='); document.cookie = `${name.trim()}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`; }); await post('/auth/logout'); }, };