Skip to content
Snippets Groups Projects
Commit 5729eb86 authored by 문경호's avatar 문경호
Browse files

Merge remote-tracking branch 'origin/feat/User-merged' into develop

parents d16eb043 9cc4d078
No related branches found
No related tags found
No related merge requests found
Pipeline #10911 failed
...@@ -2,6 +2,7 @@ const { minutesToSeconds, secondsToMinutes } = require('../utils/timeconvert'); ...@@ -2,6 +2,7 @@ const { minutesToSeconds, secondsToMinutes } = require('../utils/timeconvert');
//habittracker구조 //habittracker구조
const HabitTracker = require('../models/habittracker'); const HabitTracker = require('../models/habittracker');
const Records = require('../models/records');
//goal_weight는 habit tracker schema 안에 존재하도록 //goal_weight는 habit tracker schema 안에 존재하도록
const habittrackerController = { const habittrackerController = {
...@@ -68,6 +69,7 @@ const habittrackerController = { ...@@ -68,6 +69,7 @@ const habittrackerController = {
res.status(200).json(habitTrackergoal); res.status(200).json(habitTrackergoal);
} catch (error) { } catch (error) {
console.error(error);
res.status(500).json({ res.status(500).json({
message: 'Failed to get habitTracker goals', message: 'Failed to get habitTracker goals',
error: error.message, error: error.message,
...@@ -75,18 +77,19 @@ const habittrackerController = { ...@@ -75,18 +77,19 @@ const habittrackerController = {
} }
}, },
getEveryRecords: async (req, res) => { getEveryRecords: async (req, res) => {
const { period } = req.query.period; const { period } = req.query;
try { try {
//정규식 //정규식
const regex = new RegExp(`^${period}`); const regex = new RegExp(`^${period}`);
//해당 월에 해당하는 운동 기록들을 가져옴 //해당 월에 해당하는 운동 기록들을 가져옴
const monthlyRecords = Record.find({ const monthlyRecords = await Records.find({
user_id: req.user.user_id, //미들웨어에 있는 user_id user_id: req.user.user_id, //미들웨어에 있는 user_id
date: { $regex: regex }, date: { $regex: regex },
}); }).exec();
res.status(200).json(monthlyRecords); res.status(200).json(monthlyRecords);
} catch (error) { } catch (error) {
console.error('GetEveryRecords Error:', error);
res.status(500).json({ res.status(500).json({
message: 'Failed to get habitTracker monthly records', message: 'Failed to get habitTracker monthly records',
error: error.message, error: error.message,
......
...@@ -14,36 +14,36 @@ const timestampOptions = { ...@@ -14,36 +14,36 @@ const timestampOptions = {
}; };
const foodOptions = [ const foodOptions = [
{ name: "계란", calories: 70, carbs: 1, protein: 6, fat: 5 }, { food_id:1, name: "계란", calories: 70, carbs: 1, protein: 6, fat: 5 },
{ name: "", calories: 300, carbs: 68, protein: 6, fat: 0.5 }, { food_id:2, name: "", calories: 300, carbs: 68, protein: 6, fat: 0.5 },
{ name: "닭가슴살", calories: 120, carbs: 0, protein: 23, fat: 2 }, { food_id:3, name: "닭가슴살", calories: 120, carbs: 0, protein: 23, fat: 2 },
{ name: "사과", calories: 80, carbs: 21, protein: 0.5, fat: 0.3 }, { food_id:4, name: "사과", calories: 80, carbs: 21, protein: 0.5, fat: 0.3 },
{ name: "고구마", calories: 86, carbs: 20, protein: 1.6, fat: 0.1 }, { food_id:5, name: "고구마", calories: 86, carbs: 20, protein: 1.6, fat: 0.1 },
{ name: "바나나", calories: 89, carbs: 23, protein: 1.1, fat: 0.3 }, { food_id:6, name: "바나나", calories: 89, carbs: 23, protein: 1.1, fat: 0.3 },
{ name: "오렌지", calories: 62, carbs: 15, protein: 1.2, fat: 0.2 }, { food_id:7, name: "오렌지", calories: 62, carbs: 15, protein: 1.2, fat: 0.2 },
{ name: "소고기", calories: 250, carbs: 0, protein: 26, fat: 17 }, { food_id:8, name: "소고기", calories: 250, carbs: 0, protein: 26, fat: 17 },
{ name: "돼지고기", calories: 242, carbs: 0, protein: 27, fat: 14 }, { food_id:9, name: "돼지고기", calories: 242, carbs: 0, protein: 27, fat: 14 },
{ name: "고등어", calories: 189, carbs: 0, protein: 20, fat: 12 }, { food_id:10, name: "고등어", calories: 189, carbs: 0, protein: 20, fat: 12 },
{ name: "연어", calories: 206, carbs: 0, protein: 22, fat: 13 }, { food_id:11, name: "연어", calories: 206, carbs: 0, protein: 22, fat: 13 },
{ name: "두부", calories: 76, carbs: 1.9, protein: 8, fat: 4.8 }, { food_id:12, name: "두부", calories: 76, carbs: 1.9, protein: 8, fat: 4.8 },
{ name: "김치", calories: 33, carbs: 6.1, protein: 1.1, fat: 0.2 }, { food_id:13, name: "김치", calories: 33, carbs: 6.1, protein: 1.1, fat: 0.2 },
{ name: "우유", calories: 42, carbs: 5, protein: 3.4, fat: 1 }, { food_id:14, name: "우유", calories: 42, carbs: 5, protein: 3.4, fat: 1 },
{ name: "요거트", calories: 59, carbs: 3.6, protein: 10, fat: 0.4 }, { food_id:15, name: "요거트", calories: 59, carbs: 3.6, protein: 10, fat: 0.4 },
{ name: "치킨", calories: 239, carbs: 0, protein: 27, fat: 14 }, { food_id:16, name: "치킨", calories: 239, carbs: 0, protein: 27, fat: 14 },
{ name: "고추", calories: 40, carbs: 9, protein: 2, fat: 0.4 }, { food_id:17, name: "고추", calories: 40, carbs: 9, protein: 2, fat: 0.4 },
{ name: "양파", calories: 40, carbs: 9, protein: 1.1, fat: 0.1 }, { food_id:18, name: "양파", calories: 40, carbs: 9, protein: 1.1, fat: 0.1 },
{ name: "당근", calories: 41, carbs: 10, protein: 0.9, fat: 0.2 }, { food_id:19, name: "당근", calories: 41, carbs: 10, protein: 0.9, fat: 0.2 },
{ name: "감자", calories: 77, carbs: 17, protein: 2, fat: 0.1 }, { food_id:20, name: "감자", calories: 77, carbs: 17, protein: 2, fat: 0.1 },
{ name: "브로콜리", calories: 34, carbs: 7, protein: 2.8, fat: 0.4 }, { food_id:21, name: "브로콜리", calories: 34, carbs: 7, protein: 2.8, fat: 0.4 },
{ name: "호박", calories: 26, carbs: 6.5, protein: 1, fat: 0.1 }, { food_id:22, name: "호박", calories: 26, carbs: 6.5, protein: 1, fat: 0.1 },
{ name: "치즈", calories: 402, carbs: 1.3, protein: 25, fat: 33 }, { food_id:23, name: "치즈", calories: 402, carbs: 1.3, protein: 25, fat: 33 },
{ name: "", calories: 145, carbs: 1.3, protein: 20, fat: 7 }, { food_id:24, name: "", calories: 145, carbs: 1.3, protein: 20, fat: 7 },
{ name: "소시지", calories: 301, carbs: 2, protein: 11, fat: 28 }, { food_id:25, name: "소시지", calories: 301, carbs: 2, protein: 11, fat: 28 },
{ name: "초콜릿", calories: 546, carbs: 61, protein: 4.9, fat: 31 }, { food_id:26, name: "초콜릿", calories: 546, carbs: 61, protein: 4.9, fat: 31 },
{ name: "아몬드", calories: 576, carbs: 21, protein: 21, fat: 49 }, { food_id:27, name: "아몬드", calories: 576, carbs: 21, protein: 21, fat: 49 },
{ name: "땅콩", calories: 567, carbs: 16, protein: 25, fat: 49 }, { food_id:28, name: "땅콩", calories: 567, carbs: 16, protein: 25, fat: 49 },
{ name: "식빵", calories: 265, carbs: 49, protein: 9, fat: 3.2 }, { food_id:29, name: "식빵", calories: 265, carbs: 49, protein: 9, fat: 3.2 },
{ name: "파스타", calories: 131, carbs: 25, protein: 5, fat: 1.1 }, { food_id:30, name: "파스타", calories: 131, carbs: 25, protein: 5, fat: 1.1 },
]; ];
const initDB = async () => { const initDB = async () => {
...@@ -78,11 +78,12 @@ const initDB = async () => { ...@@ -78,11 +78,12 @@ const initDB = async () => {
} }
// Food100 컬렉션 초기화 // Food100 컬렉션 초기화
await Food100.deleteMany({}); await Food100.deleteMany();
console.log('Food100 컬렉션이 초기화되었습니다.'); console.log('Food100 컬렉션이 초기화되었습니다.');
// Food100 데이터 추가 // Food100 데이터 추가
const foodData = foodOptions.map(item => ({ const foodData = foodOptions.map(item => ({
food_id: item.food_id,
food_name: item.name, food_name: item.name,
energy_kcal: item.calories, energy_kcal: item.calories,
carbohydrate: item.carbs, carbohydrate: item.carbs,
......
...@@ -43,4 +43,4 @@ const food100Schema = new mongoose.Schema({ ...@@ -43,4 +43,4 @@ const food100Schema = new mongoose.Schema({
const Food100 = mongoose.model('Food100', food100Schema); const Food100 = mongoose.model('Food100', food100Schema);
module.exports = { Food100 }; module.exports = Food100;
\ No newline at end of file \ No newline at end of file
...@@ -35,4 +35,4 @@ const habitTrackerSchema = new mongoose.Schema( ...@@ -35,4 +35,4 @@ const habitTrackerSchema = new mongoose.Schema(
const HabitTracker = mongoose.model('HabitTracker', habitTrackerSchema); const HabitTracker = mongoose.model('HabitTracker', habitTrackerSchema);
module.exports = { HabitTracker }; module.exports = HabitTracker;
\ No newline at end of file \ No newline at end of file
...@@ -27,6 +27,6 @@ const recordSchema = new mongoose.Schema({ ...@@ -27,6 +27,6 @@ const recordSchema = new mongoose.Schema({
}, },
}); });
const Record = mongoose.model('Record', recordSchema); const Records = mongoose.model('Records', recordSchema);
module.exports = { Record }; module.exports = Records;
...@@ -162,4 +162,62 @@ async function userWithdraw(password) { ...@@ -162,4 +162,62 @@ async function userWithdraw(password) {
} }
} }
export {userLogin, userSignUp, getUserData, changeUserData, userLogout, userWithdraw}; async function refreshAccessToken() {
\ No newline at end of file try {
const response = await fetch('/api/auth/refresh', {
method: 'POST',
credentials: 'include', // refreshToken 쿠키 전송을 위해 필요
headers: {
'Content-Type': 'application/json'
}
});
if (!response.ok) {
throw new Error('토큰 갱신 실패');
}
const data = await response.json();
localStorage.setItem('accessToken', data.accessToken);
return data.accessToken;
} catch (error) {
console.error('토큰 갱신 중 에러:', error);
throw error;
}
}
async function getProfile() {
try {
let response = await fetch('/api/user/profile', {
method: 'GET',
headers: {
'Authorization': `Bearer ${localStorage.getItem('accessToken')}`,
'Content-Type': 'application/json'
}
});
// 401 에러 발생 시 토큰 갱신 시도
if (response.status === 401) {
const newAccessToken = await refreshAccessToken();
// 새로운 토큰으로 다시 요청
response = await fetch('/api/user/profile', {
method: 'GET',
headers: {
'Authorization': `Bearer ${newAccessToken}`,
'Content-Type': 'application/json'
}
});
}
if (!response.ok) {
throw new Error('프로필 조회 실패');
}
return await response.json();
} catch (error) {
console.error('프로필 조회 중 에러:', error);
throw error;
}
}
export {userLogin, userSignUp, getUserData, changeUserData, userLogout, userWithdraw, getProfile, refreshAccessToken};
\ No newline at end of file
...@@ -15,6 +15,7 @@ async function addExerciseRecord(record) { ...@@ -15,6 +15,7 @@ async function addExerciseRecord(record) {
return await fetchWithOptions('/routine/records', { return await fetchWithOptions('/routine/records', {
method: 'POST', method: 'POST',
headers: { headers: {
"Authorization": `Bearer ${localStorage.getItem('accessToken')}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify(record), body: JSON.stringify(record),
...@@ -25,6 +26,7 @@ async function getUserRoutines() { ...@@ -25,6 +26,7 @@ async function getUserRoutines() {
return await fetchWithOptions('/routine', { return await fetchWithOptions('/routine', {
method: 'GET', method: 'GET',
headers: { headers: {
"Authorization": `Bearer ${localStorage.getItem('accessToken')}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
}); });
...@@ -34,6 +36,7 @@ async function getRoutineVideos(routineName) { ...@@ -34,6 +36,7 @@ async function getRoutineVideos(routineName) {
return await fetchWithOptions(`/routine/videos?routine_name=${routineName}`, { return await fetchWithOptions(`/routine/videos?routine_name=${routineName}`, {
method: 'GET', method: 'GET',
headers: { headers: {
"Authorization": `Bearer ${localStorage.getItem('accessToken')}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
}); });
...@@ -43,6 +46,7 @@ async function deleteRoutine(routineName) { ...@@ -43,6 +46,7 @@ async function deleteRoutine(routineName) {
return await fetchWithOptions('/routine', { return await fetchWithOptions('/routine', {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
"Authorization": `Bearer ${localStorage.getItem('accessToken')}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify({ routine_name: routineName }), body: JSON.stringify({ routine_name: routineName }),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment