نسخ من RaghadAlkhous/RestaurantDash
102 أسطر
2.5 KiB
JavaScript
102 أسطر
2.5 KiB
JavaScript
import axios from 'axios';
|
|
|
|
const API_BASE_URL = 'http://127.0.0.1:8000/api';
|
|
|
|
const authService = {
|
|
|
|
register: async (email, password, confirmPassword) => {
|
|
try {
|
|
const response = await axios.post(`${API_BASE_URL}/Admin/register`, {
|
|
email,
|
|
password,
|
|
password_confirmation: confirmPassword
|
|
}, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
// في حال كان الرد يحتوي على رسائل خطأ من الباك
|
|
if (error.response) {
|
|
return {
|
|
success: false,
|
|
status: error.response.status,
|
|
message: error.response.data.message || 'Registration failed',
|
|
errors: error.response.data.errors || null
|
|
};
|
|
}
|
|
|
|
// في حال كانت المشكلة من الشبكة أو من axios نفسه
|
|
return {
|
|
success: false,
|
|
message: 'Network error. Please try again.'
|
|
};
|
|
}
|
|
},
|
|
|
|
login: async (email, password) => {
|
|
try {
|
|
const response = await axios.post(`${API_BASE_URL}/Admin/login`, {
|
|
email,
|
|
password
|
|
}, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
if (error.response) {
|
|
return {
|
|
success: false,
|
|
status: error.response.status,
|
|
message: error.response.data.message || 'Login failed',
|
|
errors: error.response.data.errors || null
|
|
};
|
|
}
|
|
|
|
return {
|
|
success: false,
|
|
message: 'Network error. Please try again.'
|
|
};
|
|
}
|
|
},
|
|
logout: async () => {
|
|
try {
|
|
const token = localStorage.getItem('token');
|
|
if (!token) {
|
|
return { success: false, message: 'No token found' };
|
|
}
|
|
|
|
const response = await axios.post(`${API_BASE_URL}/Admin/logout`, {}, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
return response.data;
|
|
} catch (error) {
|
|
if (error.response) {
|
|
return {
|
|
success: false,
|
|
status: error.response.status,
|
|
message: error.response.data.message || 'Logout failed',
|
|
};
|
|
}
|
|
return {
|
|
success: false,
|
|
message: 'Network error. Please try again.'
|
|
};
|
|
}
|
|
},
|
|
|
|
};
|
|
|
|
export default authService;
|