phone auth, image uploading, address broken up

This commit is contained in:
jackiettran
2025-07-30 19:12:56 -04:00
parent 72d79596ce
commit 7c6c120969
17 changed files with 759 additions and 182 deletions

View File

@@ -1,16 +1,16 @@
import axios from 'axios';
import axios from "axios";
const API_BASE_URL = process.env.REACT_APP_API_URL;
const api = axios.create({
baseURL: API_BASE_URL,
headers: {
'Content-Type': 'application/json',
"Content-Type": "application/json",
},
});
api.interceptors.request.use((config) => {
const token = localStorage.getItem('token');
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
@@ -22,12 +22,12 @@ api.interceptors.response.use(
(error) => {
if (error.response?.status === 401) {
// Only redirect to login if we have a token (user was logged in)
const token = localStorage.getItem('token');
const token = localStorage.getItem("token");
if (token) {
// User was logged in but token expired/invalid
localStorage.removeItem('token');
window.location.href = '/login';
localStorage.removeItem("token");
window.location.href = "/login";
}
// For non-authenticated users, just reject the error without redirecting
// Let individual components handle 401 errors as needed
@@ -37,42 +37,47 @@ api.interceptors.response.use(
);
export const authAPI = {
register: (data: any) => api.post('/auth/register', data),
login: (data: any) => api.post('/auth/login', data),
register: (data: any) => api.post("/auth/register", data),
login: (data: any) => api.post("/auth/login", data),
};
export const userAPI = {
getProfile: () => api.get('/users/profile'),
updateProfile: (data: any) => api.put('/users/profile', data),
getPublicProfile: (userId: string) => api.get(`/users/${userId}`),
getProfile: () => api.get("/users/profile"),
updateProfile: (data: any) => api.put("/users/profile", data),
uploadProfileImage: (formData: FormData) =>
api.post("/users/profile/image", formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
}),
getPublicProfile: (id: string) => api.get(`/users/${id}`),
};
export const itemAPI = {
getItems: (params?: any) => api.get('/items', { params }),
getItems: (params?: any) => api.get("/items", { params }),
getItem: (id: string) => api.get(`/items/${id}`),
createItem: (data: any) => api.post('/items', data),
createItem: (data: any) => api.post("/items", data),
updateItem: (id: string, data: any) => api.put(`/items/${id}`, data),
deleteItem: (id: string) => api.delete(`/items/${id}`),
getRecommendations: () => api.get('/items/recommendations'),
getRecommendations: () => api.get("/items/recommendations"),
};
export const rentalAPI = {
createRental: (data: any) => api.post('/rentals', data),
getMyRentals: () => api.get('/rentals/my-rentals'),
getMyListings: () => api.get('/rentals/my-listings'),
updateRentalStatus: (id: string, status: string) =>
createRental: (data: any) => api.post("/rentals", data),
getMyRentals: () => api.get("/rentals/my-rentals"),
getMyListings: () => api.get("/rentals/my-listings"),
updateRentalStatus: (id: string, status: string) =>
api.put(`/rentals/${id}/status`, { status }),
addReview: (id: string, data: any) =>
api.post(`/rentals/${id}/review`, data),
addReview: (id: string, data: any) => api.post(`/rentals/${id}/review`, data),
};
export const messageAPI = {
getMessages: () => api.get('/messages'),
getSentMessages: () => api.get('/messages/sent'),
getMessages: () => api.get("/messages"),
getSentMessages: () => api.get("/messages/sent"),
getMessage: (id: string) => api.get(`/messages/${id}`),
sendMessage: (data: any) => api.post('/messages', data),
sendMessage: (data: any) => api.post("/messages", data),
markAsRead: (id: string) => api.put(`/messages/${id}/read`),
getUnreadCount: () => api.get('/messages/unread/count'),
getUnreadCount: () => api.get("/messages/unread/count"),
};
export default api;
export default api;