From 79d9cc06a6198b90d47c49f9021a453a6ae8ba4b Mon Sep 17 00:00:00 2001 From: xfarrow <49845537+xfarrow@users.noreply.github.com> Date: Sat, 23 Mar 2024 12:23:42 +0100 Subject: [PATCH] login page enhancement --- frontend/vanilla/html/login.html | 80 +++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 7 deletions(-) diff --git a/frontend/vanilla/html/login.html b/frontend/vanilla/html/login.html index 4d8f4ae..d8e7a87 100644 --- a/frontend/vanilla/html/login.html +++ b/frontend/vanilla/html/login.html @@ -29,6 +29,14 @@ + + + +
@@ -45,12 +53,19 @@
+ placeholder="Enter a valid email address" onblur="emailFieldLosesFocus();" /> +
+ Please enter a valid e-mail +
- + +
+ Please enter your password +
@@ -83,14 +98,17 @@ loadImage(); } + /* + * This function is responsible for the log-in process + */ async function login() { const email = document.getElementById("email").value; const password = document.getElementById("password").value; if (!email || !password) { - alert('Please fill in all fields'); return; } + const response = await fetch(`${API_URL}/persons/me/token`, { method: "POST", body: JSON.stringify({ @@ -99,19 +117,21 @@ }), headers: createHeaders(null) }); - const data = await response.json(); if (response.ok) { - console.log(`Login was successful. Token is ${data.token}`); document.cookie = `token=${data.token};`; window.location.href = 'userprofile.html?id=me'; } else { - alert(data.error); - //callbackErrors(data.errors, alert); + showError(data.error); + //callbackErrors(data.errors, showError); } } + /* + * This function is responsible for loading a random stock photo seen on the + * left side of the page + */ function loadImage() { const random = Math.floor(Math.random() * 4); if (random == 0) { @@ -124,6 +144,52 @@ document.getElementById('image').src = '../content/stock_photo_4.png'; } } + + /* + * This function gets called when the e-mail field loses focus. This is + * done to inform the user whether the entered e-mail is in a valid format + */ + function emailFieldLosesFocus() { + const emailField = document.getElementById("email"); + if (!emailField.value || !validateEmail(emailField.value)) { + emailField.classList.add("is-invalid"); + } else { + emailField.classList.remove("is-invalid"); + } + } + + /* + * Validates an e-mail using a RegExpression + */ + function validateEmail(email) { + return String(email) + .toLowerCase() + .match( + /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + ); + } + + /* + * This function gets called when the password field loses focus. This is + * done to inform the user that they are required to enter a password + */ + function passwordFieldLosesFocus() { + const passwordField = document.getElementById("password"); + if (!passwordField.value) { + passwordField.classList.add("is-invalid"); + } else { + passwordField.classList.remove("is-invalid"); + } + } + + /* + * This function shows an error using a Bootstrap's alert element + */ + function showError(message) { + const alertDiv = document.getElementById('hiddenAlertMessage'); + alertDiv.innerHTML = message; + alertDiv.style.display = 'block'; + }