From de130602f93f05360a3838d93a0f9d1cbb470153 Mon Sep 17 00:00:00 2001 From: Pitu Date: Wed, 18 Jan 2017 04:51:42 -0300 Subject: Added changing of tokens from the dashboard --- public/js/panel.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++-- public/js/upload.js | 2 +- 2 files changed, 104 insertions(+), 4 deletions(-) (limited to 'public') diff --git a/public/js/panel.js b/public/js/panel.js index 8857d9c..3f99ef0 100644 --- a/public/js/panel.js +++ b/public/js/panel.js @@ -43,7 +43,7 @@ panel.verifyToken = function(token, reloadOnError = false){ } } - xhr.open('GET', '/api/token/verify', true); + xhr.open('GET', '/api/tokens/verify', true); xhr.setRequestHeader('type', 'admin'); xhr.setRequestHeader('token', token); xhr.send(null); @@ -62,6 +62,10 @@ panel.prepareDashboard = function(){ panel.getAlbums(); }); + document.getElementById('itemTokens').addEventListener('click', function(){ + panel.changeTokens(); + }); + panel.getAlbumsSidebar(); } @@ -76,7 +80,7 @@ panel.getUploads = function(album = undefined){ return panel.verifyToken(panel.token); var json = JSON.parse(xhr.responseText); - console.log(json); + if(json.success === false) return swal("An error ocurred", json.description, "error"); @@ -155,7 +159,7 @@ panel.getAlbums = function(){ return panel.verifyToken(panel.token); var json = JSON.parse(xhr.responseText); - console.log(json); + if(json.success === false) return swal("An error ocurred", json.description, "error"); @@ -261,6 +265,102 @@ panel.getAlbum = function(item){ panel.getUploads(item.id); } +panel.changeTokens = function(){ + panel.page.innerHTML = ''; + var xhr = new XMLHttpRequest(); + + var container = document.createElement('div'); + container.className = "container"; + container.innerHTML = ` +

Manage your tokens

+ + +

+ + Save +

+ + +

+ + Save +

+ `; + + xhr.onreadystatechange = function() { + if (xhr.readyState == XMLHttpRequest.DONE) { + + if(xhr.responseText === 'not-authorized') + return panel.verifyToken(panel.token); + + var json = JSON.parse(xhr.responseText); + + console.log(json); + + if(json.success === false) + return swal("An error ocurred", json.description, "error"); + + panel.page.appendChild(container); + + document.getElementById('clientToken').value = json.clientToken; + document.getElementById('adminToken').value = json.adminToken; + + document.getElementById('submitClientToken').addEventListener('click', function(){ + panel.submitToken('client', document.getElementById('clientToken').value); + }); + + document.getElementById('submitAdminToken').addEventListener('click', function(){ + panel.submitToken('admin', document.getElementById('adminToken').value); + }); + } + } + + xhr.open('GET', '/api/tokens', true); + xhr.setRequestHeader('auth', panel.token); + xhr.send(null); +} + +panel.submitToken = function(type, token){ + + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState == XMLHttpRequest.DONE) { + + if(xhr.responseText === 'not-authorized') + return panel.verifyToken(panel.token); + + var json = JSON.parse(xhr.responseText); + + console.log(json); + + if(json.success === false) + return swal("An error ocurred", json.description, "error"); + + swal({ + title: "Woohoo!", + text: 'Your token was changed successfully.', + type: "success" + }, function(){ + + if(type === 'client') + localStorage.token = token; + else if(type === 'admin') + localStorage.admintoken = token + + location.reload(); + + }) + + } + } + + xhr.open('POST', '/api/tokens/change', true); + xhr.setRequestHeader('auth', panel.token); + xhr.setRequestHeader('type', type); + xhr.setRequestHeader('token', token); + xhr.send(null); +} + window.onload = function () { panel.preparePage(); } diff --git a/public/js/upload.js b/public/js/upload.js index 0341ecc..2638ef8 100644 --- a/public/js/upload.js +++ b/public/js/upload.js @@ -58,7 +58,7 @@ upload.verifyToken = function(token, reloadOnError = false){ } } - xhr.open('GET', '/api/token/verify', true); + xhr.open('GET', '/api/tokens/verify', true); xhr.setRequestHeader('type', 'client'); xhr.setRequestHeader('token', token); xhr.send(null); -- cgit v1.2.3