Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 385a2a3a authored by 65160381's avatar 65160381
Browse files

Update file registerController.js

parent f0041e87
Branches
No related tags found
No related merge requests found
Pipeline #502 passed with warnings
......@@ -7,20 +7,35 @@ module.exports = {
},
registerUser: async (req, res) => {
const { email, rpassword } = req.body;
const { email, rpassword, confirm_password } = req.body;
// ตรวจสอบว่ารหัสผ่านทั้งสองตรงกันหรือไม่
if (rpassword !== confirm_password) {
req.flash('message', 'Passwords do not match!');
return res.redirect('/register');
}
try {
// ตรวจสอบว่าอีเมลมีอยู่ในระบบหรือไม่
const [existingUser] = await pool.execute('SELECT * FROM users WHERE email = ?', [email]);
if (existingUser.length > 0) {
req.flash('message', 'Email is already registered.');
return res.redirect('/register');
}
// แฮชรหัสผ่าน
const hashedPassword = await bcrypt.hash(rpassword, 10);
// บันทึกข้อมูลผู้ใช้ในฐานข้อมูล
const query = 'INSERT INTO users (email, password) VALUES (?, ?)';
await pool.execute(query, [email, hashedPassword]);
// ส่งข้อความแจ้งเตือนและเปลี่ยนเส้นทางไปที่หน้า login
req.flash('message', 'User registered successfully. Please log in.');
res.redirect('/login');
} catch (err) {
console.error('Error inserting user:', err);
res.status(500).send('Error occurred');
}
res.redirect('/');
}
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment