Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 1a6ad801 authored by 65160381's avatar 65160381
Browse files

Update file registerController.js

parent 77b42f58
Branches
No related tags found
No related merge requests found
Pipeline #544 passed with warnings
...@@ -9,6 +9,12 @@ module.exports = { ...@@ -9,6 +9,12 @@ module.exports = {
registerUser: async (req, res) => { registerUser: async (req, res) => {
const { email, rpassword, confirm_password } = req.body; const { email, rpassword, confirm_password } = req.body;
// ตรวจสอบข้อมูลที่จำเป็น
if (!email || !rpassword || !confirm_password) {
req.flash('message', 'กรุณากรอกข้อมูลให้ครบถ้วน');
return res.redirect('/register');
}
// ตรวจสอบว่ารหัสผ่านทั้งสองตรงกันหรือไม่ // ตรวจสอบว่ารหัสผ่านทั้งสองตรงกันหรือไม่
if (rpassword !== confirm_password) { if (rpassword !== confirm_password) {
req.flash('message', 'Passwords do not match!'); req.flash('message', 'Passwords do not match!');
...@@ -28,13 +34,21 @@ module.exports = { ...@@ -28,13 +34,21 @@ module.exports = {
// บันทึกข้อมูลผู้ใช้ในฐานข้อมูล // บันทึกข้อมูลผู้ใช้ในฐานข้อมูล
const query = 'INSERT INTO users (email, password) VALUES (?, ?)'; const query = 'INSERT INTO users (email, password) VALUES (?, ?)';
await pool.execute(query, [email, hashedPassword]); const [result] = await pool.execute(query, [email, hashedPassword]);
// ส่งข้อความแจ้งเตือนและเปลี่ยนเส้นทางไปที่หน้า login // ตรวจสอบว่ามีการบันทึกข้อมูลสำเร็จหรือไม่
if (result.affectedRows > 0) {
req.flash('message', 'Registration successful, please login.');
res.redirect('/login'); res.redirect('/login');
} else {
req.flash('message', 'เกิดข้อผิดพลาดในการสมัครสมาชิก');
res.redirect('/register');
}
} catch (err) { } catch (err) {
console.error('Error inserting user:', err); console.error('Error inserting user:', err);
res.status(500).send('Error occurred'); req.flash('message', 'เกิดข้อผิดพลาดในการสมัครสมาชิก');
res.redirect('/register');
} }
} }
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment