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
|
2018-07-17 03:13:48 +00:00
|
|
|
token: localStorage.token,
|
|
|
|
|
|
|
|
// HTML elements
|
|
|
|
user: null,
|
|
|
|
pass: null
|
2018-04-29 12:47:24 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-07-17 03:13:48 +00:00
|
|
|
page.do = function (dest, onEnter) {
|
|
|
|
var user = page.user.value
|
|
|
|
var pass = page.pass.value
|
|
|
|
|
|
|
|
// If the form is submitted with Enter button and the form is still empty
|
|
|
|
if (onEnter && !user.length && !pass.length) { return }
|
|
|
|
|
|
|
|
console.log('page.do()\'ing: ' + dest)
|
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-17 03:13:48 +00:00
|
|
|
page.formEnter = function (event) {
|
|
|
|
if (event.keyCode === 13 || event.which === 13) {
|
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
page.do('login', true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-17 03:13:48 +00:00
|
|
|
page.user = document.getElementById('user')
|
|
|
|
page.pass = document.getElementById('pass')
|
|
|
|
|
2018-07-17 03:21:04 +00:00
|
|
|
var form = document.getElementById('authForm')
|
|
|
|
form.addEventListener('keyup', page.formEnter)
|
|
|
|
form.addEventListener('keypress', page.formEnter)
|
|
|
|
form.onsubmit = function (event) {
|
2018-07-17 03:13:48 +00:00
|
|
|
event.preventDefault()
|
|
|
|
event.stopPropagation()
|
|
|
|
}
|
|
|
|
|
|
|
|
document.getElementById('loginBtn').addEventListener('click', function () {
|
|
|
|
page.do('login')
|
|
|
|
})
|
|
|
|
|
|
|
|
document.getElementById('registerBtn').addEventListener('click', function () {
|
|
|
|
page.do('register')
|
2018-05-01 14:41:25 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|