basic app
This commit is contained in:
55
src/services/authService.js
Normal file
55
src/services/authService.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import { API_CONFIG } from '../constants/api';
|
||||
|
||||
class AuthService {
|
||||
async makeRequest(endpoint, data, token = null) {
|
||||
try {
|
||||
const headers = {
|
||||
...API_CONFIG.HEADERS,
|
||||
};
|
||||
|
||||
if (token) {
|
||||
headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const response = await fetch(`${API_CONFIG.BASE_URL}${endpoint}`, {
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
const result = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(result.message || 'An error occurred');
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
throw new Error(error.message || 'Network error');
|
||||
}
|
||||
}
|
||||
|
||||
async login(phone, password) {
|
||||
return this.makeRequest(API_CONFIG.ENDPOINTS.AUTH.LOGIN, {
|
||||
phone: parseInt(phone),
|
||||
password,
|
||||
});
|
||||
}
|
||||
|
||||
async register(phone, name, password) {
|
||||
return this.makeRequest(API_CONFIG.ENDPOINTS.AUTH.REGISTER, {
|
||||
phone: parseInt(phone),
|
||||
name,
|
||||
password,
|
||||
});
|
||||
}
|
||||
|
||||
async verify(phone, code) {
|
||||
return this.makeRequest(API_CONFIG.ENDPOINTS.AUTH.VERIFY, {
|
||||
phone: parseInt(phone),
|
||||
code: parseInt(code),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export default new AuthService();
|
||||
Reference in New Issue
Block a user