@@ -45,12 +53,19 @@
@@ -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';
+ }