/
morlli
/
zavav
Обзор
Документация
Войти
/
morlli
/
zavav
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
client/src/services/authService.js
90 строк
2 KB
morlli
34
28 май 2026, 19:48
28 май 2026, 19:48
3299f08
Код
Авторство
О чём код?
const API_URL = 'https://pet-adoption-platform.onrender.com/api'; const authService = { async login(credentials) { const response = await fetch(`${API_URL}/auth/login`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(credentials), }); const data = await response.json(); if (response.ok) { localStorage.setItem('user', JSON.stringify(data)); return data; } else { throw new Error(data.message || 'Login failed'); } }, async register(userData) { const response = await fetch(`${API_URL}/auth/register`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(userData), }); const data = await response.json(); if (response.ok) { return data; } else { throw new Error(data.message || 'Registration failed'); } }, logout() { localStorage.removeItem('user'); }, getCurrentUser() { const userStr = localStorage.getItem('user'); if (userStr) { return JSON.parse(userStr); } return null; }, async requestPasswordReset(email) { const response = await fetch(`${API_URL}/auth/forgot-password`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ email }), }); const data = await response.json(); if (response.ok) { return data; } else { throw new Error(data.message || 'Password reset request failed'); } }, async resetPassword(token, newPassword) { const response = await fetch(`${API_URL}/auth/reset-password/${token}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ newPassword }), }); const data = await response.json(); if (response.ok) { return data; } else { throw new Error(data.message || 'Password reset failed'); } } }; export default authService;