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)
|
2019-08-18 09:57:56 +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)
|
2019-08-18 09:57:56 +00:00
|
|
|
return swal('An error occurred!', 'You need to specify a password.', 'error')
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
axios.post(`api/${dest}`, {
|
2019-08-26 22:00:57 +00:00
|
|
|
username: user.trim(),
|
|
|
|
password: pass.trim()
|
2018-10-09 19:52:41 +00:00
|
|
|
}).then(function (response) {
|
2018-12-18 17:01:28 +00:00
|
|
|
if (response.data.success === false)
|
2019-08-18 09:57:56 +00:00
|
|
|
return swal(`Unable to ${dest}!`, 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
|
|
|
|
2019-08-20 02:16:34 +00:00
|
|
|
page.user = document.querySelector('#user')
|
|
|
|
page.pass = document.querySelector('#pass')
|
2018-07-17 03:13:48 +00:00
|
|
|
|
2019-08-18 09:57:56 +00:00
|
|
|
// Prevent default form's submit action
|
2019-08-20 02:16:34 +00:00
|
|
|
document.querySelector('#authForm').addEventListener('submit', function (event) {
|
2019-08-18 09:57:56 +00:00
|
|
|
event.preventDefault()
|
|
|
|
})
|
|
|
|
|
2019-08-20 02:16:34 +00:00
|
|
|
document.querySelector('#loginBtn').addEventListener('click', function () {
|
2018-07-17 03:13:48 +00:00
|
|
|
page.do('login')
|
|
|
|
})
|
|
|
|
|
2019-08-20 02:16:34 +00:00
|
|
|
document.querySelector('#registerBtn').addEventListener('click', function () {
|
2018-07-17 03:13:48 +00:00
|
|
|
page.do('register')
|
2018-05-01 14:41:25 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|