diff --git a/app.js b/app.js index 1d39f3f5a89f600d58789ada5d990abbc01347e0..21c4756ee2aa58f57823f2d6fc745bb807dd53e2 100644 --- a/app.js +++ b/app.js @@ -21,12 +21,12 @@ app.use(cors({ credentials: true, })); //鍮꾨�踰덊샇 蹂�寃� �쇱슦�� �깅줉 -const passwordRoutes = require('./route/conf_password'); -//�됰꽕�� 蹂�寃� �쇱슦�� �깅줉 -const confNameRoutes = require('./route/conf_name.js'); -//�뚯썝 �덊눜 �쇱슦�� �깅줉 -const deleteUser = require('./route/del_user'); -//鍮꾨�踰덊샇 �뷀샇�붿슜 紐⑤뱢 +// const passwordRoutes = require('./route/conf_password'); +// //�됰꽕�� 蹂�寃� �쇱슦�� �깅줉 +// const confNameRoutes = require('./route/conf_name.js'); +// //�뚯썝 �덊눜 �쇱슦�� �깅줉 +// const deleteUser = require('./route/del_user'); +// //鍮꾨�踰덊샇 �뷀샇�붿슜 紐⑤뱢 const bcrypt = require('bcrypt'); //User define const User = require('./models/user'); @@ -34,15 +34,18 @@ const User = require('./models/user'); const Trip = require('./models/trips'); //Favorite define const Favorite = require('./models/favorite'); + // JSON �붿껌 蹂몃Ц�� �뚯떛�섎뒗 誘몃뱾�⑥뼱 異붽� app.use(express.json()); //鍮꾨�踰덊샇 蹂�寃� �쇱슦�� 異붽� -app.use('/conf_password',passwordRoutes); -//�대쫫 蹂�寃� �쇱슦�� -app.use('/conf_name', confNameRoutes); -//�뚯썝 �덊눜 �쇱슦�� -app.use('/del_user', deleteUser); - +// app.use('/conf_password',passwordRoutes); +// //�대쫫 蹂�寃� �쇱슦�� +// app.use('/conf_name', confNameRoutes); +// //�뚯썝 �덊눜 �쇱슦�� +// app.use('/del_user', deleteUser); +//�ъ슜�� 愿�由� �쇱슦�� +const userManagementRoutes = require('./route/userManagement'); +app.use('/userManagement', userManagementRoutes); //�ы뻾 �쇱슦�� const tripRoutes = require('./route/tripRoute'); // �쇱슦�� �뚯씪 寃쎈줈 // Routes @@ -59,9 +62,11 @@ app.use('/favorites', favoriteRoutes); const authenticateToken = require('./middleware/authMiddleware.js'); // 蹂댄샇�� �쇱슦�몄뿉 誘몃뱾�⑥뼱 �곸슜 -app.use('/conf_password', authenticateToken, passwordRoutes); -app.use('/conf_name', authenticateToken, confNameRoutes); -app.use('/del_user', authenticateToken, deleteUser); +// app.use('/conf_password', authenticateToken, passwordRoutes); +// app.use('/conf_name', authenticateToken, confNameRoutes); +// app.use('/del_user', authenticateToken, deleteUser); + +app.use('/userManagement', authenticateToken, userManagementRoutes); //�뺤씤 console.log('GOOGLE_CLIENT_ID:', process.env.GOOGLE_CLIENT_ID); @@ -173,10 +178,7 @@ app.get('/dashboard', authenticateToken, (req, res) => { // `); // }); -//signup 寃쎈줈 -app.get('/signup', (req, res) => { - res.sendFile(__dirname + '/public/signup.html'); // login.html �뚯씪 寃쎈줈 �ㅼ젙 -}); + //session // app.get('/conf_password', (req, res) => { diff --git a/controllers/userManagementController.js b/controllers/userManagementController.js new file mode 100644 index 0000000000000000000000000000000000000000..20793d08fc0cc85a80829474a07e282540e24929 --- /dev/null +++ b/controllers/userManagementController.js @@ -0,0 +1,96 @@ +const express = require('express'); +const User = require('../models/user'); +const Trip = require('../models/trips'); +const Review = require('../models/review'); +const bcrypt = require('bcrypt'); +const path = require('path'); // 寃쎈줈 泥섎━ 紐⑤뱢 異붽� + +const router = express.Router(); + +// router.get('/change', (req, res) => { + +// // �됰꽕�� 蹂�寃� HTML �뚯씪 諛섑솚 +// res.sendFile(path.join(__dirname, '../public/conf_name.html')); // HTML �뚯씪 寃쎈줈 �ㅼ젙 +// }); + +//�ъ슜�� �대쫫 蹂�寃�(�됰꽕��) +exports.changeName = async (req, res) => { + const { name, newName } = req.body; + + const user = await User.findOne({ name }); + + try { + if (!name || !newName) { + return res.status(400).json({message: '�좏슚�섏� �딆� �붿껌�낅땲��.'}); + } + + + user.name = newName; + await user.save(); + + res.status(200).json({ message: ' �대쫫�� �깃났�곸쑝濡� 蹂�寃쎈릺�덉뒿�덈떎.', user}); + console.log('�대쫫 蹂�寃� �꾨즺', user); + } catch(error) { + console.error('�대쫫 蹂�寃� �ㅻ쪟', error); + res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.'}); + } +}; + + +// 鍮꾨�踰덊샇 蹂�寃� +exports.changePassword = async (req, res) => { + const { email, oldPassword, newPassword } = req.body; + + try { + const user = await User.findOne({ email }); + if (!user || !user.password) { + return res.status(400).json({ message: '�ъ슜�먮� 李얠쓣 �� �녾굅�� �섎せ�� �붿껌�낅땲��.' }); + } + + const isMatch = await bcrypt.compare(oldPassword, user.password); + if (!isMatch) { + return res.status(400).json({ message: '湲곗〈 鍮꾨�踰덊샇媛� �쇱튂�섏� �딆뒿�덈떎.' }); + } + + const hashedPassword = await bcrypt.hash(newPassword, 10); + user.password = hashedPassword; + + await user.save(); + res.status(200).json({ message: '鍮꾨�踰덊샇媛� �깃났�곸쑝濡� 蹂�寃쎈릺�덉뒿�덈떎.' }); + console.log('鍮꾨�踰덊샇 蹂�寃� �꾨즺',user); + } catch (error) { + console.error('鍮꾨�踰덊샇 蹂�寃� �ㅻ쪟:', error); + res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); + } +}; + +// �ъ슜�� ��젣 +exports.deleteUser = async (req, res) => { + const { userId } = req.params; + + try { + // �ъ슜�� ��젣 + const user = await User.findByIdAndDelete(userId); + if (!user) { + return res.status(404).json({ message: '�ъ슜�먮� 李얠쓣 �� �놁뒿�덈떎.' }); + } + + // �ъ슜�먭� �앹꽦�� �ы뻾 �곗씠�� ��젣 �먮뒗 �곹깭 蹂�寃� + await Trip.deleteMany({ create_by: userId }); + //�ъ슜�먭� �묒꽦�� 由щ럭 �쒓굅 + await Review.deleteMany({ create_by: userId}); + + // �ъ슜�먭� 怨듬룞 �묒뾽�먮줈 �ы븿�� 寃쎌슦 �낅뜲�댄듃 + await Trip.updateMany( + { collaborators: userId }, //�꾪꽣留� - 吏��곕젮�� �좎� �꾩씠�붽� 怨듬룞 �묒뾽�먮줈 �ы븿�쒓꼍�� + { $pull: { collaborators: userId } } // 怨듬룞 �묒뾽�� 紐⑸줉�먯꽌 �쒓굅 + ); + console.log('�� �ъ슜�먮� ��젣�⑸땲��: ',user); + res.status(200).json({ message: '�ъ슜�먭� �깃났�곸쑝濡� ��젣�섏뿀�듬땲��.' }); + } catch (error) { + console.error('�ъ슜�� ��젣 �ㅻ쪟:', error); + res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); + } +}; + + diff --git a/models/trips.js b/models/trips.js index 0cc86fdd0d0a90a4c8fb0f3ecdab0e6d2faf0a25..3d8b1841b5ee659c25c41a1ef0cb8a7e5e84b62b 100644 --- a/models/trips.js +++ b/models/trips.js @@ -61,9 +61,10 @@ tripSchema.pre('save', function (next) { const start = new Date(trip.start_date); const end = new Date(trip.end_date); - // 湲곗〈 `plans` �곗씠�곕� �좎� - const plans = trip.plans || new Map(); - + // // 湲곗〈 `plans` �곗씠�곕� �좎� + // const plans = trip.plans || new Map(); + // plans媛� �녿뒗 寃쎌슦 珥덇린�� + const plans = trip.plans instanceof Map ? trip.plans : new Map(Object.entries(trip.plans || {})); let currentDay = 1; // �� day留� 異붽� diff --git a/route/conf_name.js b/route/conf_name.js deleted file mode 100644 index 06c88e00fbfb8003d6557c9017d4a41d0cf4b806..0000000000000000000000000000000000000000 --- a/route/conf_name.js +++ /dev/null @@ -1,36 +0,0 @@ -const express = require('express'); -const User = require('../models/user'); -const bcrypt = require('bcrypt'); -const path = require('path'); // 寃쎈줈 泥섎━ 紐⑤뱢 異붽� - -const router = express.Router(); -router.get('/change', (req, res) => { - - // �됰꽕�� 蹂�寃� HTML �뚯씪 諛섑솚 - res.sendFile(path.join(__dirname, '../public/conf_name.html')); // HTML �뚯씪 寃쎈줈 �ㅼ젙 -}); - -//�ъ슜�� �대쫫 蹂�寃�(�됰꽕��) -router.post('/change', async (req, res) => { - const { name, newName } = req.body; - - const user = await User.findOne({ name }); - - try { - if (!name || !newName) { - return res.status(400).json({message: '�좏슚�섏� �딆� �붿껌�낅땲��.'}); - } - - - user.name = newName; - await user.save(); - - res.status(200).json({ message: ' �대쫫�� �깃났�곸쑝濡� 蹂�寃쎈릺�덉뒿�덈떎.', user}); - console.log('�대쫫 蹂�寃� �꾨즺', user); - } catch(error) { - console.error('�대쫫 蹂�寃� �ㅻ쪟', error); - res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.'}); - } -}); - -module.exports = router; \ No newline at end of file diff --git a/route/conf_password.js b/route/conf_password.js deleted file mode 100644 index 9d91354138cb869d43b583c0124f69f7c8414948..0000000000000000000000000000000000000000 --- a/route/conf_password.js +++ /dev/null @@ -1,40 +0,0 @@ -const express = require('express'); -const bcrypt = require('bcrypt'); -const User = require('../models/user'); // User 紐⑤뜽 媛��몄삤湲� -const path = require('path'); // 寃쎈줈 泥섎━ 紐⑤뱢 異붽� - -const router = express.Router(); - -router.get('/change', (req, res) => { - - // 鍮꾨�踰덊샇 蹂�寃� HTML �뚯씪 諛섑솚 - res.sendFile(path.join(__dirname, '../public/conf_password.html')); // HTML �뚯씪 寃쎈줈 �ㅼ젙 -}); -// 鍮꾨�踰덊샇 蹂�寃� 濡쒖쭅 -router.post('/change', async (req, res) => { - const { email, oldPassword, newPassword } = req.body; - - try { - const user = await User.findOne({ email }); - if (!user || !user.password) { - return res.status(400).json({ message: '�ъ슜�먮� 李얠쓣 �� �녾굅�� �섎せ�� �붿껌�낅땲��.' }); - } - - const isMatch = await bcrypt.compare(oldPassword, user.password); - if (!isMatch) { - return res.status(400).json({ message: '湲곗〈 鍮꾨�踰덊샇媛� �쇱튂�섏� �딆뒿�덈떎.' }); - } - - const hashedPassword = await bcrypt.hash(newPassword, 10); - user.password = hashedPassword; - - await user.save(); - res.status(200).json({ message: '鍮꾨�踰덊샇媛� �깃났�곸쑝濡� 蹂�寃쎈릺�덉뒿�덈떎.' }); - console.log('鍮꾨�踰덊샇 蹂�寃� �꾨즺',user); - } catch (error) { - console.error('鍮꾨�踰덊샇 蹂�寃� �ㅻ쪟:', error); - res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); - } -}); - -module.exports = router; diff --git a/route/del_user.js b/route/del_user.js deleted file mode 100644 index 27dad7aa4a53b24c912f25ef914a4baae34ab2b5..0000000000000000000000000000000000000000 --- a/route/del_user.js +++ /dev/null @@ -1,38 +0,0 @@ -const express = require('express'); -const User = require('../models/user'); -const Trip = require('../models/trips'); -const Review = require('../models/review'); - -const router = express.Router(); - -// �ъ슜�� ��젣 -router.delete('/:userId', async (req, res) => { - const { userId } = req.params; - - try { - // �ъ슜�� ��젣 - const user = await User.findByIdAndDelete(userId); - if (!user) { - return res.status(404).json({ message: '�ъ슜�먮� 李얠쓣 �� �놁뒿�덈떎.' }); - } - - // �ъ슜�먭� �앹꽦�� �ы뻾 �곗씠�� ��젣 �먮뒗 �곹깭 蹂�寃� - await Trip.deleteMany({ create_by: userId }); - //�ъ슜�먭� �묒꽦�� 由щ럭 �쒓굅 - //�ъ슜�먭� ��젣�섎㈃ ��젣�섎굹..? - await Review.deleteMany({ create_by: userId}); - - // �ъ슜�먭� 怨듬룞 �묒뾽�먮줈 �ы븿�� 寃쎌슦 �낅뜲�댄듃 - await Trip.updateMany( - { collaborators: userId }, //�꾪꽣留� - 吏��곕젮�� �좎� �꾩씠�붽� 怨듬룞 �묒뾽�먮줈 �ы븿�쒓꼍�� - { $pull: { collaborators: userId } } // 怨듬룞 �묒뾽�� 紐⑸줉�먯꽌 �쒓굅 - ); - console.log('�� �ъ슜�먮� ��젣�⑸땲��: ',user); - res.status(200).json({ message: '�ъ슜�먭� �깃났�곸쑝濡� ��젣�섏뿀�듬땲��.' }); - } catch (error) { - console.error('�ъ슜�� ��젣 �ㅻ쪟:', error); - res.status(500).json({ message: '�쒕쾭 �ㅻ쪟媛� 諛쒖깮�덉뒿�덈떎.' }); - } -}); - -module.exports = router; diff --git a/route/userManagement.js b/route/userManagement.js new file mode 100644 index 0000000000000000000000000000000000000000..5695f907b72df37b1a0acb0b3a3e4368ecbc8234 --- /dev/null +++ b/route/userManagement.js @@ -0,0 +1,11 @@ +const express = require('express'); +const router = express.Router(); +const User = require('../models/user'); // User �ㅽ궎留� 李몄“ + +const userManagementController = require('../controllers/userManagementController'); + +router.post('/change', userManagementController.changeName); +router.post('/changePassword', userManagementController.changePassword); +router.delete('/:userId', userManagementController.deleteUser); + +module.exports = router;