diff --git a/public/js/auth.js b/public/js/auth.js index 9013298..68cf9e3 100644 --- a/public/js/auth.js +++ b/public/js/auth.js @@ -14,19 +14,19 @@ page.do = function(dest){ username: user, password: pass }) - .then(function (response) { + .then(function (response) { - if(response.data.success === false) - return swal('Error', response.data.description, 'error'); - - localStorage.token = response.data.token; - window.location('/panel'); + if(response.data.success === false) + return swal('Error', response.data.description, 'error'); + + localStorage.token = response.data.token; + window.location = '/panel'; - }) - .catch(function (error) { - return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); - console.log(error); - }); + }) + .catch(function (error) { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); } page.verify = function(){ @@ -36,18 +36,18 @@ page.verify = function(){ axios.post('/api/tokens/verify', { token: page.token }) - .then(function (response) { + .then(function (response) { - if(response.data.success === false) - return swal('Error', response.data.description, 'error'); - - window.location('/panel'); + if(response.data.success === false) + return swal('Error', response.data.description, 'error'); + + window.location = '/panel'; - }) - .catch(function (error) { - return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); - console.log(error); - }); + }) + .catch(function (error) { + return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error'); + console.log(error); + }); } diff --git a/public/js/panel.js b/public/js/panel.js index 4df7d5d..8337cd4 100644 --- a/public/js/panel.js +++ b/public/js/panel.js @@ -1,17 +1,11 @@ let panel = {} panel.page; -panel.token = localStorage.admintoken; +panel.token = localStorage.token; panel.filesView = localStorage.filesView; panel.preparePage = function(){ - if(!panel.token){ - document.getElementById('auth').style.display = 'flex'; - document.getElementById('tokenSubmit').addEventListener('click', function(){ - panel.verifyToken(document.getElementById('token').value); - }); - return; - } + if(!panel.token) return window.location = '/auth'; panel.verifyToken(panel.token, true); } @@ -20,7 +14,6 @@ panel.verifyToken = function(token, reloadOnError){ reloadOnError = false; axios.post('/api/tokens/verify', { - type: 'admin', token: token }) .then(function (response) { @@ -32,15 +25,15 @@ panel.verifyToken = function(token, reloadOnError){ type: "error" }, function(){ if(reloadOnError){ - localStorage.removeItem("admintoken"); - location.reload(); + localStorage.removeItem("token"); + location.location = '/auth'; } }) return; } - axios.defaults.headers.common['auth'] = token; - localStorage.admintoken = token; + axios.defaults.headers.common['token'] = token; + localStorage.token = token; panel.token = token; return panel.prepareDashboard(); @@ -73,7 +66,7 @@ panel.prepareDashboard = function(){ } panel.logout = function(){ - localStorage.removeItem("admintoken"); + localStorage.removeItem("token"); location.reload('/'); } @@ -85,14 +78,12 @@ panel.getUploads = function(album = undefined, page = undefined){ if(album !== undefined) url = '/api/album/' + album + '/' + page - axios.get(url) - .then(function (response) { + axios.get(url).then(function (response) { if(response.data.success === false){ - if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token); + if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); else return swal("An error ocurred", response.data.description, "error"); } - var prevPage = 0; var nextPage = page + 1; @@ -125,9 +116,7 @@ panel.getUploads = function(album = undefined, page = undefined){ if(panel.filesView === 'thumbs'){ - container.innerHTML = ` - ${pagination}
- - Save -
- - -- - Save + + Request new token
`; panel.page.appendChild(container); - document.getElementById('clientToken').value = response.data.clientToken; - document.getElementById('adminToken').value = response.data.adminToken; - - document.getElementById('submitClientToken').addEventListener('click', function(){ - panel.submitToken('client', document.getElementById('clientToken').value); + document.getElementById('getNewToken').addEventListener('click', function(){ + panel.getNewToken(); }); - document.getElementById('submitAdminToken').addEventListener('click', function(){ - panel.submitToken('admin', document.getElementById('adminToken').value); - }); - - }) .catch(function (error) { return swal("An error ocurred", 'There was an error with the request, please check the console for more information.', "error"); @@ -534,16 +499,13 @@ panel.changeTokens = function(){ } -panel.submitToken = function(type, token){ +panel.getNewToken = function(){ - axios.post('/api/tokens/change', { - type: type, - token: token - }) + axios.post('/api/tokens/change') .then(function (response) { if(response.data.success === false){ - if(response.data.description === 'not-authorized') return panel.verifyToken(panel.token); + if(response.data.description === 'No token provided') return panel.verifyToken(panel.token); else return swal("An error ocurred", response.data.description, "error"); } @@ -552,14 +514,8 @@ panel.submitToken = function(type, token){ text: 'Your token was changed successfully.', type: "success" }, function(){ - - if(type === 'client') - localStorage.token = token; - else if(type === 'admin') - localStorage.admintoken = token - + localStorage.token = response.data.token; location.reload(); - }) }) diff --git a/public/js/upload.js b/public/js/upload.js index b620248..b52f670 100644 --- a/public/js/upload.js +++ b/public/js/upload.js @@ -36,7 +36,6 @@ upload.verifyToken = function(token, reloadOnError){ reloadOnError = false; axios.post('/api/tokens/verify', { - type: 'client', token: token }) .then(function (response) { @@ -101,7 +100,7 @@ upload.prepareDropzone = function(){ maxFiles: 1000, autoProcessQueue: true, headers: { - 'auth': upload.token + 'token': upload.token }, init: function() { this.on('addedfile', function(file) {