Gitlab@Informatics

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

Update 4 files

- /controllers/registerController.js
- /controllers/loginController.js
- /views/register.ejs
- /views/login.ejs
parent d74bcf8f
No related branches found
No related tags found
No related merge requests found
Pipeline #571 passed with warnings
......@@ -7,7 +7,7 @@ module.exports = {
},
loginUser: async (req, res) => {
const { email, password } = req.body; // เปลี่ยนจาก rpassword เป็น password เพื่อให้ชัดเจน
const { email, rpassword } = req.body;
try {
const [rows] = await pool.execute('SELECT * FROM users WHERE email = ?', [email]);
if (rows.length === 0) {
......@@ -16,20 +16,18 @@ module.exports = {
}
const user = rows[0];
// ตรวจสอบรหัสผ่าน
const match = await bcrypt.compare(password, user.password); // เปลี่ยน rpassword เป็น password
const match = await bcrypt.compare(rpassword, user.password);
if (match) {
req.session.userId = user.user_id; // ใช้ user_id แทน id
res.redirect('/'); // ถ้าการเข้าสู่ระบบสำเร็จจะ redirect ไปที่หน้า home
req.session.userId = user.id
req.session.userIdEmail = user.email;
res.redirect('/');
} else {
req.flash('message', 'Password incorrect');
return res.redirect('/login'); // ถ้ารหัสผ่านไม่ตรง
return res.redirect('/login');
}
} catch (err) {
console.error(err);
req.flash('message', 'Server error, please try again later');
res.status(500).redirect('/login'); // ถ้าเกิดข้อผิดพลาดในการเชื่อมต่อฐานข้อมูล
res.status(500).send('Server error');
}
}
};
// ในไฟล์ registerController.js
const bcrypt = require('bcrypt');
const pool = require('../db');
exports.showRegisterPage = (req, res) => {
res.render('register'); // ส่งค่าไปยังไฟล์ EJS ที่เป็นหน้า register
};
module.exports = {
showRegisterPage: (req, res) => {
res.render('register', { message: req.flash('message') });
},
exports.registerUser = async (req, res) => {
registerUser: async (req, res) => {
const { email, rpassword } = req.body;
// ตรวจสอบข้อมูล เช่น ชื่อผู้ใช้และรหัสผ่าน
// สมมติว่าเราทำการเก็บข้อมูลผู้ใช้ใน DB
try {
// ตัวอย่างการสมัคร (สมมติว่ามีการเก็บข้อมูลใน DB)
// สมมติว่าผู้ใช้สมัครสำเร็จ
req.flash('messages', 'Registration successful!');
const hashedPassword = await bcrypt.hash(rpassword, 10);
const query = 'INSERT INTO users (email, password) VALUES (?, ?)';
await pool.execute(query, [email, hashedPassword]);
req.flash('message', 'User registered successfully. Please log in.');
res.redirect('/login');
} catch (error) {
req.flash('messages', 'Error occurred during registration. Please try again later.');
res.redirect('/register');
} catch (err) {
console.error('Error inserting user:', err);
res.status(500).send('Error occurred');
}
}
};
......@@ -129,14 +129,14 @@
</svg>
<main class="form-signin w-100 m-auto">
<% if (message) { %>
<div class="alert alert-danger">
<form action="/user/login" method="POST">
<% if (message && message.length > 0) { %>
<div class="alert alert-danger" role="alert">
<%= message %>
</div>
<% } %>
<form action="/user/login" method="POST">
<h1 class="h3 mb-3 fw-normal">Please sign in</h1>
<h1 class="h3 mb-3 fw-normal">Sign in</h1>
<div class="form-floating">
<label for="floatingInput">Email address</label>
......@@ -144,7 +144,7 @@
</div>
<div class="form-floating">
<label for="floatingPassword">Password</label>
<input type="password" class="form-control" id="floatingPassword" name="password" placeholder="Password">
<input type="password" class="form-control" id="floatingPassword" name="rpassword" placeholder="Password">
</div>
<div class="form-check text-start my-3">
......
......@@ -130,16 +130,6 @@
<main class="form-signin w-100 m-auto">
<% if (messages.length > 0) { %>
<div class="alert alert-danger">
<ul>
<% messages.forEach(function(message) { %>
<li><%= message %></li>
<% }); %>
</ul>
</div>
<% } %>
<form action="/user/register" method="POST">
<h1 class="h3 mb-3 fw-normal">Sign up</h1>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment