filesafe/public/js/auth.js
Bobby Wibowo 56bdd08ee7
Init 'browser-ecma5' branch (#7)
* Downgraded ecma version of client-side scripts to v5. This change means no more backtick strings and some others.

* Massively modified auth.js, dashboard.js and home.js to support the downgrade (dashboard.js had the most changes).

* Removed enter key event handler from auth page. The previous code had some small issues. I'd rather not have the handler than let the issues persist. I'll eventually look into adding this again in the future.

* Updated uploadController.js to handle some invalid requests into /api/delete and /api/bulkdelete.
2018-09-04 22:49:37 +07:00

75 lines
1.7 KiB
JavaScript

/* global swal, axios */
var page = {
// user token
token: localStorage.token,
// HTML elements
user: null,
pass: null
}
page.do = function (dest) {
var user = page.user.value
var pass = page.pass.value
if (!user) {
return swal('Error', 'You need to specify a username', 'error')
}
if (!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 = 'dashboard'
})
.catch(function (error) {
console.error(error)
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
}
page.verify = function () {
if (!page.token) { 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 = 'dashboard'
})
.catch(function (error) {
console.error(error)
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
}
window.onload = function () {
page.verify()
page.user = document.getElementById('user')
page.pass = document.getElementById('pass')
document.getElementById('loginBtn').addEventListener('click', function () {
page.do('login')
})
document.getElementById('registerBtn').addEventListener('click', function () {
page.do('register')
})
}