Xây dựng trang đăng ký đơn giản với Nodejs express và mysql

Lượt xem: 4551

Ở bài trước dandev cùng các bạn tìm hiểu và xây dựng trang login đơn giản với nodejs kết nối với mysql. Để có thể nhập thì mình cần phải có account đúng không ạ? Chính vì vậy bài viết này dandev sẽ cùng các bạn xây dựng trang đăng ký đơn giản trong nodejs có kết nối với mysql nhé.
 

Mình sẽ vẫn tận dụng css của form login. Nên chúng ta sẽ tạo thêm file register.html với nội dung sau


 <!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,minimum-scale=1">
		<title>Register</title>
        <!-- the form awesome library is used to add icons to our form -->
		<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.1/css/all.css">
        <!-- include the stylesheet file -->
        <link href="/style.css" rel="stylesheet" type="text/css">
	</head>
	<body>
		<div class="login">
			<h1>Register</h1>
			<form action="/auth/register" method="post">
				<label for="email">					
					<i class="fas fa-envelope"></i>
				</label>
				<input type="text" name="email" placeholder="Email" id="email" required>
				<label for="username">					
					<i class="fas fa-user"></i>
				</label>
				<input type="text" name="username" placeholder="Username" id="username" required>
				<label for="password">
					<i class="fas fa-lock"></i>
				</label>
				<input type="password" name="password" placeholder="Password" id="password" required>
				<input type="submit" value="Register">
			</form>
		</div>
	</body>
</html>

Tiếp theo trong file login.js mình bổ sung thêm. Để khi chúng ta truy cập vào đường dẫn http://localhost:3000/resiger thì hiển thị form register


 app.get('/register', function(request, response) {
	// Render login template
	response.sendFile(path.join(__dirname + '/register.html'));
});


Bổ sung thêm đoạn code sau để lấy thông tin từ form register và thêm thông tin đăng ký vào database


 // http://localhost:3000/auth/register
app.post('/auth/register', function(request, response) {
	// Capture the input fields
	let username = request.body.username;
	let password = request.body.password;
    let email = request.body.email;
	// Ensure the input fields exists and are not empty
	if (username && password && email) {
		// Execute SQL query that'll select the account from the database based on the specified username and password
		connection.query('SELECT * FROM accounts WHERE username = ?', [username], function(error, results, fields) {
			// If there is an issue with the query, output the error
			if (error) throw error;
			// If the account exists
			if (results.length > 0) {
				// Authenticate the user
				response.send('Username has existed!');
			} else {
				connection.query('INSERT INTO accounts(username, password, email) VALUES(?,?,?)', [username, password, email ], function(error, results) {
                    // If there is an issue with the query, output the error
                    if (error) throw error;
                    // If the account exists
                               
                });
                response.send('Register success!');
			}			
			response.end();
		});
	} else {
		response.send('Please enter Username and Password!');
		response.end();
	}
});

Video thao tác cùng dandev