From 1a6ad801ca7bb3a46399eb51ed9a45dd8e3b0491 Mon Sep 17 00:00:00 2001
From: 65160381 <65160381@go.buu.ac.th>
Date: Mon, 24 Mar 2025 14:23:35 +0000
Subject: [PATCH] Update file registerController.js

---
 controllers/registerController.js | 22 ++++++++++++++++++----
 1 file changed, 18 insertions(+), 4 deletions(-)

diff --git a/controllers/registerController.js b/controllers/registerController.js
index dfc085f..8b25697 100644
--- a/controllers/registerController.js
+++ b/controllers/registerController.js
@@ -9,6 +9,12 @@ module.exports = {
     registerUser: async (req, res) => {
         const { email, rpassword, confirm_password } = req.body;
 
+        // ตรวจสอบข้อมูลที่จำเป็น
+        if (!email || !rpassword || !confirm_password) {
+            req.flash('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
+            return res.redirect('/register');
+        }
+
         // ตรวจสอบว่ารหัสผ่านทั้งสองตรงกันหรือไม่
         if (rpassword !== confirm_password) {
             req.flash('message', 'Passwords do not match!');
@@ -28,13 +34,21 @@ module.exports = {
 
             // บันทึกข้อมูลผู้ใช้ในฐานข้อมูล
             const query = 'INSERT INTO users (email, password) VALUES (?, ?)';
-            await pool.execute(query, [email, hashedPassword]);
+            const [result] = await pool.execute(query, [email, hashedPassword]);
+
+            // ตรวจสอบว่ามีการบันทึกข้อมูลสำเร็จหรือไม่
+            if (result.affectedRows > 0) {
+                req.flash('message', 'Registration successful, please login.');
+                res.redirect('/login');
+            } else {
+                req.flash('message', 'เกิดข้อผิดพลาดในการสมัครสมาชิก');
+                res.redirect('/register');
+            }
 
-            // ส่งข้อความแจ้งเตือนและเปลี่ยนเส้นทางไปที่หน้า login
-            res.redirect('/login');
         } catch (err) {
             console.error('Error inserting user:', err);
-            res.status(500).send('Error occurred');
+            req.flash('message', 'เกิดข้อผิดพลาดในการสมัครสมาชิก');
+            res.redirect('/register');
         }
     }
 };
-- 
GitLab