نسخ من RaghadAlkhous/RestaurantDash
Linking of login, logout and registration interfaces.
هذا الالتزام موجود في:
101
src/services/authService.js
Normal file
101
src/services/authService.js
Normal file
@@ -0,0 +1,101 @@
|
||||
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;
|
||||
المرجع في مشكلة جديدة
حظر مستخدم