From 90cf2810d5bace1292146fdc5e650d3efe8f2f5c Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Tue, 3 Dec 2024 15:04:36 +0700 Subject: [PATCH] Create app.js --- src/ui/app.js | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/ui/app.js diff --git a/src/ui/app.js b/src/ui/app.js new file mode 100644 index 000000000..deb149ae5 --- /dev/null +++ b/src/ui/app.js @@ -0,0 +1,100 @@ +// src/ui/app.js + +document.addEventListener('DOMContentLoaded', () => { + const registerForm = document.getElementById('register-form'); + const loginForm = document.getElementById('login-form'); + + // Function to display messages + function displayMessage(element, message, isSuccess) { + const messageElement = document.createElement('div'); + messageElement.textContent = message; + messageElement.className = isSuccess ? 'success-message' : 'error-message'; + element.appendChild(messageElement); + setTimeout(() => { + messageElement.remove(); + }, 3000); + } + + // Validate form inputs + function validateForm(username, password) { + if (!username || !password) { + return 'Username and password are required.'; + } + if (password.length < 6) { + return 'Password must be at least 6 characters long.'; + } + return null; + } + + // Handle user registration + if (registerForm) { + registerForm.addEventListener('submit', async (e) => { + e.preventDefault(); + const username = document.getElementById('register-username').value; + const password = document.getElementById('register-password').value; + + const validationError = validateForm(username, password); + if (validationError) { + displayMessage(registerForm, validationError, false); + return; + } + + try { + const response = await fetch('/register', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }); + + const data = await response.json(); + if (response.ok) { + displayMessage(registerForm, 'Registrationsuccessful!', true); + registerForm.reset(); + } else { + displayMessage(registerForm, `Error: ${data.error}`, false); + } + } catch (error) { + console.error('Error:', error); + displayMessage(registerForm, 'An error occurred during registration.', false); + } + }); + } + + // Handle user login + if (loginForm) { + loginForm.addEventListener('submit', async (e) => { + e.preventDefault(); + const username = document.getElementById('login-username').value; + const password = document.getElementById('login-password').value; + + const validationError = validateForm(username, password); + if (validationError) { + displayMessage(loginForm, validationError, false); + return; + } + + try { + const response = await fetch('/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ username, password }), + }); + + const data = await response.json(); + if (response.ok) { + displayMessage(loginForm, 'Login successful! Token: ' + data.token, true); + loginForm.reset(); + } else { + displayMessage(loginForm, `Error: ${data.error}`, false); + } + } catch (error) { + console.error('Error:', error); + displayMessage(loginForm, 'An error occurred during login.', false); + } + }); + } +});