From 7cf8376e7b4c1ad4247ed8bad03bb2bae852f399 Mon Sep 17 00:00:00 2001
From: 65160381 <65160381@go.buu.ac.th>
Date: Tue, 25 Mar 2025 01:19:52 +0700
Subject: [PATCH] 7.8

---
 app.js | 72 +++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 43 insertions(+), 29 deletions(-)

diff --git a/app.js b/app.js
index 4a17df5..8e8724b 100644
--- a/app.js
+++ b/app.js
@@ -106,43 +106,57 @@ app.post('/register', async (req, res) => {
 });
 
 
+// Route สำหรับการล็อกอิน
 app.post('/login', async (req, res) => {
-    const { email, password } = req.body;
-
-    if (!email || !password) {
-        return res.status(400).json({ error: 'All fields are required' });
-    }
-
+    const { email, password } = req.body;  // ใช้ email แทน user
     try {
+        const connection = await pool.getConnection();
         // ค้นหาผู้ใช้จาก email
-        const [user] = await pool.query(
+        const [rows] = await connection.query(
             'SELECT * FROM users WHERE email = ?',
             [email]
         );
-
-        if (user.length === 0) {
-            return res.status(404).json({ error: 'User not found' });
-        }
-
-        // ตรวจสอบรหัสผ่าน
-        const isMatch = await bcrypt.compare(password, user[0].password);
-
-        if (!isMatch) {
-            return res.status(401).json({ error: 'Invalid password' });
+        connection.release();
+  
+        if (rows.length > 0) {
+            // ตรวจสอบรหัสผ่าน
+            const match = await bcrypt.compare(password, rows[0].password);
+            if (match) {
+                // เก็บข้อมูลผู้ใช้ใน session (แค่ user_id และ email)
+                req.session.user = {
+                    id: rows[0].user_id,     // user_id ของผู้ใช้
+                    email: rows[0].email     
+                };
+                console.log("User session:", req.session.user);  // เพิ่ม console log เพื่อตรวจสอบ
+                res.redirect('/');  // ไปที่หน้า home หรือหน้าแรกหลังจากล็อกอิน
+                return;
+            } else {
+                res.status(400).send('Invalid password');
+                return;
+            }
+        } else {
+            res.status(400).send('User not found');
+            return;
         }
-
-        // สร้าง session ให้กับผู้ใช้
-        req.session.user = {
-            id: user[0].user_id,
-            email: user[0].email
-        };
-
-        res.status(200).json({ message: 'Login successful', user: req.session.user });
-    } catch (error) {
-        console.error('❌ Login failed:', error);
-        res.status(500).json({ error: 'Login failed' });
+    } catch (err) {
+        console.error('Login error:', err);
+        res.status(500).send('Login failed');
+        return;
     }
-});
+  });
+  
+  // API สำหรับดึงข้อมูลผู้ใช้หลังจากล็อกอิน
+  app.get('/api/getUser', (req, res) => {
+    if (req.session.user) {
+        res.json({
+            user_id: req.session.user.id,  // ส่งข้อมูล user_id จาก session
+            email: req.session.user.email  // ส่งข้อมูล email จาก session
+        });
+    } else {
+        res.status(401).send('User not logged in');
+    }
+  });
+  
 
 app.use(express.json()); // สำหรับการ parse ข้อมูลแบบ JSON
 app.use(express.urlencoded({ extended: true })); // สำหรับการ parse ข้อมูลแบบ URL-encoded
-- 
GitLab