Added more logout functionality.

main
Luna 2021-07-24 11:27:09 -07:00
parent cb636f047f
commit 446d74daf2
1 changed files with 31 additions and 5 deletions

View File

@ -1,9 +1,9 @@
//VARIBLES
// //IF NOT LOGGED IN DON'T SHOW LOG IN BUTTON
// if (username === '') {
// document.getElementById("logoutlink").style.display = "none";
// }
myStorage = window.localStorage;
// //IF NOT LOGGED IN DON'T SHOW LOGOUT BUTTON
while (username === null) {
document.getElementById("logoutbutton").style.display = "none";
}
//LOGOUT FETCH FUNCTION
@ -21,3 +21,29 @@ async function logout() {
username = null;
loggedIn()
}
//CHECKS TO SEE IF USERNAME MATCHES TOKEN
let tokenUpdate = window.setInterval(checkToken, 1000);
async function checkToken() {
const response = await fetch(`api/token/${username}/`);
const matches = await response.json();
//YES THIS IS CONFUSING I KNOW.
if (matches.status === "fail") {
loggedOut()
}
// IF NO USERNAME BUT HAS A TOKEN THEN LOGOUT
if (matches.status === "ok" && myStorage.length === 0) {
logout()
}
}
//AND IF THEY DON'T HAVE A TOKEN CLEARS THE LOCAL STORED USERNAME
function loggedOut() {
localStorage.removeItem('username')
document.querySelector("#loggeduser").innerHTML = 'You are not logged in'
}