2018-01-23 20:06:30 +00:00
|
|
|
/* global swal, axios */
|
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
var page = {
|
2018-04-29 12:47:24 +00:00
|
|
|
// user token
|
|
|
|
token: localStorage.token
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
page.do = function (dest) {
|
|
|
|
var user = document.getElementById('user').value
|
|
|
|
var pass = document.getElementById('pass').value
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
if (!user) {
|
2018-01-23 20:06:30 +00:00
|
|
|
return swal('Error', 'You need to specify a username', 'error')
|
|
|
|
}
|
2018-04-29 12:47:24 +00:00
|
|
|
|
|
|
|
if (!pass) {
|
2018-01-23 20:06:30 +00:00
|
|
|
return swal('Error', 'You need to specify a username', 'error')
|
|
|
|
}
|
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
axios.post(`api/${dest}`, {
|
2018-01-23 20:06:30 +00:00
|
|
|
username: user,
|
|
|
|
password: pass
|
|
|
|
})
|
2018-07-14 03:42:18 +00:00
|
|
|
.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) {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.error(error)
|
2018-03-30 02:39:53 +00:00
|
|
|
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
|
2018-03-19 16:51:39 +00:00
|
|
|
})
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
page.verify = function () {
|
2018-04-29 12:47:24 +00:00
|
|
|
if (!page.token) { return }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
axios.post('api/tokens/verify', {
|
2018-01-23 20:06:30 +00:00
|
|
|
token: page.token
|
|
|
|
})
|
2018-07-14 03:42:18 +00:00
|
|
|
.then(function (response) {
|
|
|
|
if (response.data.success === false) {
|
|
|
|
return swal('Error', response.data.description, 'error')
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location = 'dashboard'
|
|
|
|
})
|
|
|
|
.catch(function (error) {
|
2018-05-09 08:41:30 +00:00
|
|
|
console.error(error)
|
2018-07-14 03:42:18 +00:00
|
|
|
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
|
2018-03-19 16:51:39 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
window.onload = function () {
|
2018-01-23 20:06:30 +00:00
|
|
|
page.verify()
|
2018-05-01 14:41:25 +00:00
|
|
|
|
2018-07-14 03:42:18 +00:00
|
|
|
document.body.addEventListener('keydown', function (event) {
|
2018-05-01 14:41:25 +00:00
|
|
|
event = event || window.event
|
|
|
|
if (!event) { return }
|
2018-07-14 03:42:18 +00:00
|
|
|
var id = event.target.id
|
2018-05-01 14:41:25 +00:00
|
|
|
if (!['user', 'pass'].includes(id)) { return }
|
|
|
|
if (event.keyCode === 13 || event.which === 13) { page.do('login') }
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|