diff --git a/frontend/js/login.js b/frontend/js/login.js index 837fb2b..42b6c58 100644 --- a/frontend/js/login.js +++ b/frontend/js/login.js @@ -17,3 +17,25 @@ document.querySelector('form').addEventListener('submit', () => { const username = document.getElementById('username').value; localStorage.setItem('username', username); }) + +document.getElementById('loginForm').addEventListener('submit', async (e) => { + e.preventDefault() + const username = document.getElementById('username').value.trim(); + const password = document.getElementById('password').value.trim(); + const messageBox = document.getElementById('messageBox'); + const jsonData = JSON.stringify({ username, password }) + const response = await fetch ('/auth/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: jsonData + }); + + const result = await response.json(); + if(response.ok) { + location.href='/chat'; + } else { + messageBox.innerText = result.message; + } +}) \ No newline at end of file diff --git a/frontend/js/settings.js b/frontend/js/settings.js index 7b26e28..ffb81cc 100644 --- a/frontend/js/settings.js +++ b/frontend/js/settings.js @@ -1,5 +1,4 @@ const logoutButton = document.getElementById('logout'); -const messageBox = document.getElementById('messageBox'); logoutButton.onclick = logout; function logout() { @@ -28,7 +27,7 @@ document.getElementById('changePasswordForm').addEventListener('submit', async ( e.preventDefault(); const cPassword = document.getElementById('cPassword').value.trim(); const nPassword = document.getElementById('nPassword').value.trim(); - + const messageBox = document.getElementById('messageBox'); const jsonData = JSON.stringify({ cPassword, nPassword }); const response = await fetch('/auth/changepassword', { method: 'POST', @@ -38,6 +37,6 @@ document.getElementById('changePasswordForm').addEventListener('submit', async ( body: jsonData }); const result = await response.json(); - // display result message (successful or no) + // Display result message messageBox.innerText = result.message; }) \ No newline at end of file diff --git a/frontend/js/signup.js b/frontend/js/signup.js index 50f0b80..3b3d4ce 100644 --- a/frontend/js/signup.js +++ b/frontend/js/signup.js @@ -16,13 +16,29 @@ function showPasswd() { document.getElementById('signupForm').addEventListener('submit',async function (event) { event.preventDefault(); + const messageBox = document.getElementById('messageBox'); + const username = document.getElementById('username').value.trim(); + const password = document.getElementById("password").value.trim(); + const sPassword = document.getElementById("sPassword").value.trim(); + const jsonData = JSON.stringify({ username, password }); - const password = document.getElementById("password").value; - const sPassword = document.getElementById("sPassword").value; - console.log(password, sPassword); if(password !== sPassword){ - alert("Passwords don't match!"); + messageBox.innerText = "Passwords don't match!" + return; + } + + const response = await fetch ('/auth/signup', { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: jsonData + }); + + const result = await response.json(); + if(response.ok) { + location.href='/chat'; } else { - this.submit(); + messageBox.innerText = result.message; } }) diff --git a/frontend/routes/login.html b/frontend/routes/login.html index df14f2b..32e8393 100644 --- a/frontend/routes/login.html +++ b/frontend/routes/login.html @@ -13,7 +13,7 @@