From 8523ee6ac091775f77a1c7d4c0c9a93a9d08c78e Mon Sep 17 00:00:00 2001 From: Pitu Date: Sun, 29 Jan 2017 04:20:16 -0300 Subject: Created auth view --- public/js/auth.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 public/js/auth.js (limited to 'public/js/auth.js') diff --git a/public/js/auth.js b/public/js/auth.js new file mode 100644 index 0000000..9013298 --- /dev/null +++ b/public/js/auth.js @@ -0,0 +1,56 @@ +var page = {}; + +page.do = function(dest){ + + var user = document.getElementById('user').value; + var pass = document.getElementById('pass').value; + + if(user === undefined || user === null || user === '') + return swal('Error', 'You need to specify a username', 'error'); + if(pass === undefined || pass === null || pass === '') + return swal('Error', 'You need to specify a username', 'error'); + + axios.post('/api/' + dest, { + username: user, + password: pass + }) + .then(function (response) { + + 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); + }); +} + +page.verify = function(){ + page.token = localStorage.token; + if(page.token === undefined) return; + + axios.post('/api/tokens/verify', { + token: page.token + }) + .then(function (response) { + + 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); + }); + +} + +window.onload = function () { + page.verify(); +} \ No newline at end of file -- cgit v1.2.3 From b781237454e7db4c5cd5395c0198fe9c384751ca Mon Sep 17 00:00:00 2001 From: Pitu Date: Mon, 30 Jan 2017 04:41:44 -0300 Subject: Replaced auth header with token --- public/js/auth.js | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'public/js/auth.js') 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) { - - 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); - }); + .then(function (response) { + + 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); + }); } page.verify = function(){ @@ -36,18 +36,18 @@ page.verify = function(){ axios.post('/api/tokens/verify', { token: page.token }) - .then(function (response) { - - 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); - }); + .then(function (response) { + + 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); + }); } -- cgit v1.2.3