diff --git a/controllers/registerController.js b/controllers/registerController.js index dfc085fdc66eb45fcbe0b64c76e83b51450cac53..26be4c929845ce9a611c04c795496d16f94ff95d 100644 --- a/controllers/registerController.js +++ b/controllers/registerController.js @@ -7,7 +7,7 @@ module.exports = { }, registerUser: async (req, res) => { - const { email, rpassword, confirm_password } = req.body; + const { email, username, fname, lname, rpassword, confirm_password } = req.body; // ตรวจสอบว่ารหัสผ่านทั้งสองตรงกันหรือไม่ if (rpassword !== confirm_password) { @@ -23,12 +23,19 @@ module.exports = { return res.redirect('/register'); } + // ตรวจสอบว่า username มีอยู่แล้วหรือไม่ + const [existingUsername] = await pool.execute('SELECT * FROM users WHERE username = ?', [username]); + if (existingUsername.length > 0) { + req.flash('message', 'Username is already taken.'); + 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]); + const query = 'INSERT INTO users (email, username, password, fname, lname) VALUES (?, ?, ?, ?, ?)'; + await pool.execute(query, [email, username, hashedPassword, fname, lname]); // ส่งข้อความแจ้งเตือนและเปลี่ยนเส้นทางไปที่หน้า login res.redirect('/login'); diff --git a/views/register.ejs b/views/register.ejs index 86678518292f5f9b4335055c8f08840cea58847e..785895f56f42e071b81b88c8b2173d13e16c0959 100644 --- a/views/register.ejs +++ b/views/register.ejs @@ -129,28 +129,27 @@ </svg> <main class="form-signin w-100 m-auto"> - <form action="/user/register" method="POST"> - <h1 class="h3 mb-3 fw-normal">Sign up</h1> + <form method="POST" action="/register"> + <label for="email">Email:</label> + <input type="email" name="email" required> + + <label for="username">Username:</label> + <input type="text" name="username" required> + + <label for="fname">First Name:</label> + <input type="text" name="fname" required> + + <label for="lname">Last Name:</label> + <input type="text" name="lname" required> - <div class="form-floating"> - <label for="floatingInput">Email address</label> - <input type="email" class="form-control" id="floatingInput" name="email" placeholder="name@example.com"> - </div> + <label for="rpassword">Password:</label> + <input type="password" name="rpassword" required> - <div class="form-floating"> - <label for="floatingPassword">Password</label> - <input type="password" class="form-control" id="floatingPassword" name="rpassword" placeholder="Password"> - </div> + <label for="confirm_password">Confirm Password:</label> + <input type="password" name="confirm_password" required> - <div class="form-floating"> - <label for="floatingConfirmPassword">Confirm Password</label> - <input type="password" class="form-control" id="floatingConfirmPassword" name="confirm_password" placeholder="Confirm Password"> - </div> - - - - <button class="btn btn-primary w-100 py-2" type="submit">Sign up</button> - </form> + <button type="submit">Register</button> + </form> </main> <script src="/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>