filesafe/public/js/auth.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-01-29 07:20:16 +00:00
var page = {};
page.do = function(dest) {
2017-01-29 07:20:16 +00:00
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'); }
2017-01-29 07:20:16 +00:00
axios.post(`/api/${dest}`, {
2017-01-29 07:20:16 +00:00
username: user,
password: pass
})
.then(response => {
if (response.data.success === false) { return swal('Error', response.data.description, 'error'); }
localStorage.token = response.data.token;
window.location = '/dashboard';
})
.catch(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() {
2017-01-29 07:20:16 +00:00
page.token = localStorage.token;
if (page.token === undefined) return;
2017-01-29 07:20:16 +00:00
axios.post('/api/tokens/verify', { token: page.token })
.then(response => {
if (response.data.success === false) { return swal('Error', response.data.description, 'error'); }
2017-01-29 07:20:16 +00:00
window.location = '/dashboard';
})
.catch(error => {
return swal('An error ocurred', 'There was an error with the request, please check the console for more information.', 'error');
console.log(error);
});
};
2017-01-29 07:20:16 +00:00
window.onload = function() {
2017-01-29 07:20:16 +00:00
page.verify();
};