2018-01-23 20:06:30 +00:00
|
|
|
/* global swal, axios */
|
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
const 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-09-04 15:49:37 +00:00
|
|
|
page.do = function (dest) {
|
2018-10-09 19:52:41 +00:00
|
|
|
const user = page.user.value
|
|
|
|
const pass = page.pass.value
|
2018-07-17 03:13:48 +00:00
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!user)
|
2018-10-09 19:52:41 +00:00
|
|
|
return swal('An error occurred!', 'You need to specify a username', 'error')
|
2018-04-29 12:47:24 +00:00
|
|
|
|
2018-12-18 17:01:28 +00:00
|
|
|
if (!pass)
|
2018-10-09 19:52:41 +00:00
|
|
|
return swal('An error occurred!', 'You need to specify a username', 'error')
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
axios.post(`api/${dest}`, {
|
2018-01-23 20:06:30 +00:00
|
|
|
username: user,
|
|
|
|
password: pass
|
2018-10-09 19:52:41 +00:00
|
|
|
}).then(function (response) {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (response.data.success === false)
|
2018-10-09 19:52:41 +00:00
|
|
|
return swal('An error occurred!', response.data.description, 'error')
|
2018-07-14 03:42:18 +00:00
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
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')
|
|
|
|
})
|
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-12-18 17:01:28 +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-10-09 19:52:41 +00:00
|
|
|
}).then(function (response) {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (response.data.success === false)
|
2018-10-09 19:52:41 +00:00
|
|
|
return swal('An error occurred!', response.data.description, 'error')
|
2018-07-14 03:42:18 +00:00
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
window.location = 'dashboard'
|
|
|
|
}).catch(function (error) {
|
2019-07-03 15:56:45 +00:00
|
|
|
console.log(error)
|
|
|
|
const description = error.response.data && error.response.data.description
|
|
|
|
? error.response.data.description
|
|
|
|
: 'There was an error with the request, please check the console for more information.'
|
|
|
|
return swal(`${error.response.status} ${error.response.statusText}`, description, 'error')
|
2018-10-09 19:52:41 +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-17 03:13:48 +00:00
|
|
|
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')
|
2018-05-01 14:41:25 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|