import axios from 'axios'; import AsyncStorage from '@react-native-async-storage/async-storage'; // API 인스턴스 생성 const apiInstance = axios.create({ baseURL: 'http://takensoftai.iptime.org:15857', headers: { 'Content-Type': 'application/json', } }); const ApiUtils = { register: async (userData) => { try { const response = await apiInstance.post('/auth/register', userData); return response.data; } catch (error) { console.error('Register error:', error.response || error); throw error.response?.data || error; } }, login: async (credentials) => { try { const response = await apiInstance.post('/auth/login', credentials); return response.data; } catch (error) { console.error('Login error:', error.response || error); throw error.response?.data || error; } }, uploadPhoto: async (uri) => { const formData = new FormData(); formData.append('photo', { uri, type: 'image/jpeg', name: 'photo.jpg', }); try { const response = await apiInstance.post('/upload', formData, { headers: { 'Content-Type': 'multipart/form-data', }, }); return response.data; } catch (error) { console.error('Upload photo error:', error.response || error); throw error.response?.data || error; } }, sendTripLog: async (data, navigation) => { console.log(data); try { // AsyncStorage에서 토큰 가져오기 const tokenString = await AsyncStorage.getItem('token'); if (tokenString) { const tokenObject = JSON.parse(tokenString); const accessToken = tokenObject.accessToken; if (!accessToken) { await AsyncStorage.removeItem('token'); // 로그인 페이지로 리디렉션 navigation.navigate('Login'); return; } // 데이터 전송 try { const response = await apiInstance.post('/action/gps_update', data, { headers: { 'Content-Type': 'application/json', Authorization: `${accessToken}`, // Bearer 스키마를 사용하도록 수정 }, }); return response.data; } catch (error) { console.error('Send trip log error:', error.response?.status, error.response?.data || error.message); throw error.response?.data || error; } } else { console.log("토큰이 스토리지에 없습니다."); // 로그인 페이지로 리디렉션 navigation.navigate('Login'); } } catch (error) { console.error('Token retrieval error:', error.message); // 로그인 페이지로 리디렉션 navigation.navigate('Login'); } } }; export default ApiUtils;