nbd-app
Version:
🚀 CLI tool to scaffold full-stack authentication starter projects with React, Node.js, and multiple auth providers (Email, Google, and more)
79 lines (70 loc) • 1.74 kB
JavaScript
import axios from 'axios';
// Create an axios instance with base URL from environment variables
const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
withCredentials: true, // Important for sending/receiving cookies
headers: {
'Content-Type': 'application/json',
},
});
// Handle API errors
const handleApiError = (error) => {
const message =
error.response?.data?.message ||
error.message ||
'Something went wrong';
return {
message,
status: error.response?.status || 500
};
};
// Auth API services
export const authService = {
// Register a new user
signup: async (userData) => {
try {
const response = await api.post('/auth/signup', userData);
return response.data;
} catch (error) {
throw handleApiError(error);
}
},
// Login user
login: async (credentials) => {
try {
const response = await api.post('/auth/login', credentials);
return response.data;
} catch (error) {
throw handleApiError(error);
}
},
// Logout user
logout: async () => {
try {
const response = await api.get('/auth/logout');
return response.data;
} catch (error) {
throw handleApiError(error);
}
},
// Get current user
getCurrentUser: async () => {
try {
const response = await api.get('/auth/me');
return response.data;
} catch (error) {
throw handleApiError(error);
}
},
// Update user profile
// Update this function
updateProfile: async (userData) => {
try {
const response = await api.put('/auth/update-profile', userData);
return response.data;
} catch (error) {
throw handleApiError(error);
}
}
};
export default api;