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/css/style.css | 4 +- public/js/panel.js | 112 +++++++++++++++++++++++++++++++-------------------- public/js/upload.js | 49 ++++++++++++---------- 3 files changed, 99 insertions(+), 66 deletions(-) (limited to 'public') diff --git a/public/css/style.css b/public/css/style.css index ad67545..b8d69ee 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -82,11 +82,13 @@ img.logo { height: 200px; margin-top: 20px; } ------------------ */ section#dashboard { display: none } +section#auth input { background: rgba(0, 0, 0, 0); } section#auth input, section#auth a { border-left: 0px; border-top: 0px; border-right: 0px; border-radius: 0px; - background: rgba(0, 0, 0, 0); box-shadow: 0 0 0; } + +section#dashboard .table { font-size: 12px } 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(); + } + } diff --git a/public/js/upload.js b/public/js/upload.js index bb60f28..c99ecb4 100644 --- a/public/js/upload.js +++ b/public/js/upload.js @@ -8,7 +8,7 @@ window.onload = function () { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { - USINGTOKEN = JSON.parse(xhr.responseText).token; + USINGTOKEN = JSON.parse(xhr.responseText).private; prepareTokenThing(); } } @@ -20,14 +20,14 @@ window.onload = function () { if(!USINGTOKEN) return getInfo(); if(!localStorage.token){ - document.getElementById('tokenContainer').style.display = 'flex' document.getElementById('tokenSubmit').addEventListener('click', function(){ getInfo(document.getElementById('token').value) }); - }else{ - getInfo(localStorage.token); + return document.getElementById('tokenContainer').style.display = 'flex'; } + getInfo(localStorage.token); + } function prepareDropzone(){ @@ -91,23 +91,25 @@ window.onload = function () { xhr.onreadystatechange = function() { if (xhr.readyState == XMLHttpRequest.DONE) { - if(xhr.responseText !== 'not-authorized'){ - - div = document.createElement('div'); - div.id = 'dropzone'; - div.innerHTML = 'Click here or drag and drop files'; - div.style.display = 'flex'; - - document.getElementById('btnGithub').style.display = 'none'; - document.getElementById('tokenContainer').style.display = 'none'; - document.getElementById('uploadContainer').appendChild(div); - document.getElementById('panel').style.display = 'block'; - - if(xhr.responseText.maxFileSize) maxSize = JSON.parse(xhr.responseText).maxFileSize; - if(token) localStorage.token = token; - - prepareDropzone(); - } + + if(xhr.responseText === 'not-authorized') + return notAuthorized(); + + div = document.createElement('div'); + div.id = 'dropzone'; + div.innerHTML = 'Click here or drag and drop files'; + div.style.display = 'flex'; + + document.getElementById('btnGithub').style.display = 'none'; + document.getElementById('tokenContainer').style.display = 'none'; + document.getElementById('uploadContainer').appendChild(div); + document.getElementById('panel').style.display = 'block'; + + if(xhr.responseText.maxFileSize) maxSize = JSON.parse(xhr.responseText).maxFileSize; + if(token) localStorage.token = token; + + prepareDropzone(); + } } xhr.open('GET', '/api/info', true); @@ -117,4 +119,9 @@ window.onload = function () { xhr.send(null); } + + function notAuthorized() { + localStorage.removeItem("token"); + location.reload(); + } }; \ No newline at end of file -- cgit v1.2.3