Added more logout functionality.
This commit is contained in:
		
							parent
							
								
									cb636f047f
								
							
						
					
					
						commit
						446d74daf2
					
				
					 1 changed files with 31 additions and 5 deletions
				
			
		
							
								
								
									
										36
									
								
								logout.js
									
									
									
									
									
								
							
							
						
						
									
										36
									
								
								logout.js
									
									
									
									
									
								
							|  | @ -1,9 +1,9 @@ | ||||||
| //VARIBLES
 | //VARIBLES
 | ||||||
| 
 | myStorage = window.localStorage; | ||||||
| // //IF NOT LOGGED IN DON'T SHOW LOG IN BUTTON
 | // //IF NOT LOGGED IN DON'T SHOW LOGOUT BUTTON
 | ||||||
| //  if (username === '') {
 | while (username === null) { | ||||||
| //     document.getElementById("logoutlink").style.display = "none";
 |   document.getElementById("logoutbutton").style.display = "none"; | ||||||
| //  }
 | } | ||||||
| 
 | 
 | ||||||
| //LOGOUT FETCH FUNCTION
 | //LOGOUT FETCH FUNCTION
 | ||||||
| 
 | 
 | ||||||
|  | @ -21,3 +21,29 @@ async function logout() { | ||||||
|   username = null; |   username = null; | ||||||
|   loggedIn() |   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' | ||||||
|  | } | ||||||
		Loading…
	
		Reference in a new issue