import { auth } from '../instance/axiosInstance';
export const register = async (formData) => {
try {
const response = await auth.post(`/register`, formData);
return response.data;
} catch (error) {
const errorMessage = error?.response?.data?.message || '회원가입 중 오류가 발생했습니다.';
console.log(errorMessage);
alert(errorMessage);
throw new Error(errorMessage);
}
};
export const login = async (formData) => {
try {
const response = await auth.post(`/login?expiresIn=60m`, formData);
localStorage.setItem('accessToken', response.data.accessToken);
return response.data;
} catch (error) {
console.log(error?.response?.data.message);
alert(error?.response?.data.message);
}
};
export const getUserInfo = async () => {
const accessToken = localStorage.getItem('accessToken');
if (accessToken) {
try {
const response = await auth.get(`/user`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`
}
});
return response.data;
} catch (error) {
console.log(error?.response?.data.message);
alert('accessToken이 만료되었습니다');
localStorage.clear();
}
}
};
export const updateProfile = async (formData) => {
console.log(formData);
const accessToken = localStorage.getItem('accessToken');
if (accessToken) {
try {
const response = await auth.patch(`/profile`, formData, {
headers: {
'Content-Type': 'multipart/form-data',
Authorization: `Bearer ${accessToken}`
}
});
return response.data;
} catch (error) {
console.log(error?.response?.data.message);
alert(error?.response?.data.message);
localStorage.clear();
}
}
};