2018-01-23 20:06:30 +00:00
|
|
|
/* global swal, axios */
|
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
const page = {}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
page.do = dest => {
|
|
|
|
const user = document.getElementById('user').value
|
|
|
|
const pass = document.getElementById('pass').value
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
if (user === undefined || user === null || user === '') {
|
|
|
|
return swal('Error', 'You need to specify a username', 'error')
|
|
|
|
}
|
|
|
|
if (pass === undefined || pass === null || pass === '') {
|
|
|
|
return swal('Error', 'You need to specify a username', 'error')
|
|
|
|
}
|
|
|
|
|
2018-01-24 15:31:23 +00:00
|
|
|
axios.post('api/' + dest, {
|
2018-01-23 20:06:30 +00:00
|
|
|
username: user,
|
|
|
|
password: pass
|
|
|
|
})
|
2018-03-28 11:36:28 +00:00
|
|
|
.then(response => {
|
2018-03-19 16:51:39 +00:00
|
|
|
if (response.data.success === false) {
|
|
|
|
return swal('Error', response.data.description, 'error')
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.token = response.data.token
|
|
|
|
window.location = 'dashboard'
|
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.log(error)
|
2018-03-19 16:51:39 +00:00
|
|
|
return swal('An error occurred', 'There was an error with the request, please check the console for more information.', 'error')
|
|
|
|
})
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-03-19 16:51:39 +00:00
|
|
|
page.onkeypress = function (event, element) {
|
|
|
|
event = event || window.event
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!event) { return }
|
2018-03-28 11:36:28 +00:00
|
|
|
if (event.keyCode === 13 || event.which === 13) {
|
|
|
|
return this.do('login')
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
page.verify = () => {
|
2018-01-23 20:06:30 +00:00
|
|
|
page.token = localStorage.token
|
2018-03-28 17:40:50 +00:00
|
|
|
if (page.token === undefined) { return }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-01-24 15:31:23 +00:00
|
|
|
axios.post('api/tokens/verify', {
|
2018-01-23 20:06:30 +00:00
|
|
|
token: page.token
|
|
|
|
})
|
2018-03-28 11:36:28 +00:00
|
|
|
.then(response => {
|
2018-03-19 16:51:39 +00:00
|
|
|
if (response.data.success === false) {
|
|
|
|
return swal('Error', response.data.description, 'error')
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location = 'dashboard'
|
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.log(error)
|
2018-03-19 16:51:39 +00:00
|
|
|
return swal('An error occurred', 'There was an error with the request, please check the console for more information.', 'error')
|
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 11:36:28 +00:00
|
|
|
window.onload = () => {
|
2018-01-23 20:06:30 +00:00
|
|
|
page.verify()
|
|
|
|
}
|