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/upload.js | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) (limited to 'public/js/upload.js') 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