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');
|
|
|
|
|
|
|
|
const cookies = document.cookie //TEMP
|
|
|
|
|
|
|
|
// 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!')
|
|
|
|
document.querySelector("#username").innerHTML = `${uname}`
|
|
|
|
document.querySelector("#errormessage").innerHTML = ''
|
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-17 22:50:58 +00:00
|
|
|
}
|
2021-07-22 14:01:28 +00:00
|
|
|
|
|
|
|
console.log(cookies) //TEMP
|