Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 5f5d9139 authored by 65160394's avatar 65160394
Browse files

Project Round 5

parent b1da3496
No related branches found
No related tags found
No related merge requests found
...@@ -74,13 +74,67 @@ exports.postLogin = async (req, res) => { ...@@ -74,13 +74,67 @@ exports.postLogin = async (req, res) => {
} }
}; };
exports.Logout = (req, res) => { exports.Logout = (req, res) => {
req.session.destroy(() => { req.session.destroy(() => {
res.redirect('/'); res.redirect('/');
}); });
}; };
//Profile
exports.getProfilePage = async (req, res) => {
if (!req.session.userId) {
return res.redirect('/login');
}
try {
const user = await User.findById(req.session.userId);
res.render('profile', { user });
} catch (err) {
console.error(err);
res.redirect('/');
}
};
exports.getEditProfilePage = async (req, res) => {
if (!req.session.userId) {
return res.redirect('/login');
}
try {
const user = await User.findById(req.session.userId);
res.render('edit-profile', { user });
} catch (err) {
console.error(err);
res.redirect('/profile');
}
};
exports.updateProfile = async (req, res) => {
const { name, email, password } = req.body;
if (!name || !email) {
return res.status(400).send('Name and Email are required');
}
try {
let user = await User.findById(req.session.userId);
// อัปเดตชื่อและอีเมล
user.name = name;
user.email = email;
// อัปเดตพาสเวิร์ดถ้ามีการกรอก
if (password) {
user.password = await bcrypt.hash(password, 10);
}
await user.save();
res.redirect('/profile');
} catch (err) {
console.error(err);
res.status(500).send('Error updating profile');
}
};
//CRUD //CRUD
// แสดงฟอร์มสร้างทัวร์ // แสดงฟอร์มสร้างทัวร์
exports.getCreateTour = (req, res) => { exports.getCreateTour = (req, res) => {
......
...@@ -28,7 +28,7 @@ router.get('/delete/:id', tourController.deleteTour); //ลบทัวร์ ...@@ -28,7 +28,7 @@ router.get('/delete/:id', tourController.deleteTour); //ลบทัวร์
router.get('/search', tourController.searchTours); //หน้าค้นหาทัวร์ router.get('/search', tourController.searchTours); //หน้าค้นหาทัวร์
//Profile //Profile
router.get('/profile', userController.getProfilePage);//หน้าโปรไฟล์ router.get('/profile', tourController.getProfilePage);//หน้าโปรไฟล์
router.post('/edit-profile', userController.updateProfile);// อัปเดตข้อมูลโปรไฟล์ router.post('/edit-profile', tourController.updateProfile);// อัปเดตข้อมูลโปรไฟล์
module.exports = router; module.exports = router;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment