2021-07-22 14:01:28 +00:00
|
|
|
|
|
|
|
//SETTING VARIABLES. GRABBING CONTENTS FROM FORM.
|
|
|
|
|
|
|
|
let uname = document.querySelector('#uname').value;
|
|
|
|
let pin = document.querySelector('#pin').value;
|
|
|
|
const form = document.querySelector('form');
|
|
|
|
|
|
|
|
// SUBMIT FORM FUNCTION. AND FETCH USERNAME AND PIN FROM API.
|
|
|
|
|
|
|
|
form.addEventListener("submit", async function (event) {
|
|
|
|
event.preventDefault();
|
|
|
|
const formData = new FormData(form);
|
|
|
|
|
|
|
|
uname = formData.get('uname');
|
|
|
|
pin = formData.get('pin');
|
|
|
|
|
|
|
|
const response = await fetch(`/api/users/${uname}/${pin}`);
|
|
|
|
const loginInfo = await response.json();
|
|
|
|
|
|
|
|
if (loginInfo.status === "ok") {
|
|
|
|
login()
|
|
|
|
} else {
|
|
|
|
incorrectLogin()
|
|
|
|
}
|
2021-07-17 22:50:58 +00:00
|
|
|
})
|
|
|
|
|
2021-07-22 14:01:28 +00:00
|
|
|
|
|
|
|
// FUNCTIONS FOR WHETHER THE LOGIN WAS A SUCCESS OR FAILURE
|
|
|
|
|
2021-07-17 22:50:58 +00:00
|
|
|
function login() {
|
2021-07-22 14:01:28 +00:00
|
|
|
console.log('You have logged in!')
|
2021-07-23 06:51:26 +00:00
|
|
|
document.querySelector("#username").innerHTML = `Logged in as ${uname}`
|
2021-07-22 14:01:28 +00:00
|
|
|
document.querySelector("#errormessage").innerHTML = ''
|
2021-07-22 20:09:02 +00:00
|
|
|
localStorage.setItem("username", `${uname}`);
|
2021-07-17 22:50:58 +00:00
|
|
|
}
|
2021-07-22 14:01:28 +00:00
|
|
|
|
2021-07-17 22:50:58 +00:00
|
|
|
function incorrectLogin() {
|
2021-07-22 14:01:28 +00:00
|
|
|
console.log('Incorrect Login!')
|
|
|
|
document.querySelector("#errormessage").innerHTML = 'Incorrect Login.'
|
2021-07-22 20:09:02 +00:00
|
|
|
}
|