From bdfd512c10986a9b4f137e668be6bd80dbd8f617 Mon Sep 17 00:00:00 2001 From: pitu Date: Tue, 17 Jan 2017 00:37:54 -0300 Subject: token handling and verification --- public/js/panel.js | 112 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 44 deletions(-) (limited to 'public/js/panel.js') diff --git a/public/js/panel.js b/public/js/panel.js index ed1bea0..cff5609 100644 --- a/public/js/panel.js +++ b/public/js/panel.js @@ -1,15 +1,11 @@ window.onload = function () { - if(!localStorage.admintoken){ - askForToken(); - return; - } + var page; - var dashboard = document.getElementById('dashboard'); - var page = document.getElementById('page'); + if(!localStorage.admintoken) + return askForToken(); - dashboard.style.display = 'block'; - prepareMenu(); + prepareDashboard(); function askForToken(){ document.getElementById('tokenSubmit').addEventListener('click', function(){ @@ -21,15 +17,35 @@ window.onload = function () { xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { + try{ + + var json = JSON.parse(xhr.responseText); + if(json.success === false) + return alert(json.description); + + localStorage.admintoken = document.getElementById('token').value; + prepareDashboard(); + + }catch(e){ + console.log(e); + } + + console.log(xhr.responseText); // xhr.responseText } } - xhr.open('POST', '/api/info', true); + xhr.open('GET', '/api/verify', true); + xhr.setRequestHeader('type', 'admin'); + xhr.setRequestHeader('token', document.getElementById('token').value); xhr.send(null); } } - function prepareMenu(){ + function prepareDashboard(){ + page = document.getElementById('page'); + document.getElementById('auth').style.display = 'none'; + document.getElementById('dashboard').style.display = 'block'; + document.getElementById('itemUploads').addEventListener('click', function(){ getUploads(); }); @@ -45,44 +61,47 @@ window.onload = function () { xhr.onreadystatechange = function() { if(xhr.readyState == XMLHttpRequest.DONE){ - if(xhr.responseText !== 'not-authorized'){ - var json = JSON.parse(xhr.responseText); - - var container = document.createElement('div'); - container.innerHTML = ` - - - - - - - - - - -
FileGalleryDate
`; - page.appendChild(container); - - var table = document.getElementById('table'); - - for(var item of json){ - - var tr = document.createElement('tr'); - tr.innerHTML = ` - - ${item.file} - ${item.gallery} - ${item.date} - - `; - - table.appendChild(tr); - } + + if(xhr.responseText === 'not-authorized') + return notAuthorized(); + + var json = JSON.parse(xhr.responseText); + + var container = document.createElement('div'); + container.innerHTML = ` + + + + + + + + + + +
FileGalleryDate
`; + page.appendChild(container); + + var table = document.getElementById('table'); + + for(var item of json){ + + var tr = document.createElement('tr'); + tr.innerHTML = ` + + ${item.file} + ${item.gallery} + ${item.date} + + `; + + table.appendChild(tr); } + } } xhr.open('GET', '/api/uploads', true); - xhr.setRequestHeader('auth', localStorage.token); + xhr.setRequestHeader('auth', localStorage.admintoken); xhr.send(null); } @@ -93,4 +112,9 @@ window.onload = function () { } + function notAuthorized() { + localStorage.removeItem("admintoken"); + location.reload(); + } + } -- cgit v1.2.3