2018-01-23 20:06:30 +00:00
|
|
|
/* global swal, axios */
|
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
const page = {
|
|
|
|
// user token
|
|
|
|
token: localStorage.token
|
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
page.do = async dest => {
|
2018-03-28 11:36:28 +00:00
|
|
|
const user = document.getElementById('user').value
|
|
|
|
const 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-04-29 12:47:24 +00:00
|
|
|
const response = await axios.post(`api/${dest}`, {
|
2018-01-23 20:06:30 +00:00
|
|
|
username: user,
|
|
|
|
password: pass
|
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.log(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-04-29 12:47:24 +00:00
|
|
|
if (!response) { return }
|
|
|
|
|
|
|
|
if (response.data.success === false) {
|
|
|
|
return swal('Error', response.data.description, 'error')
|
|
|
|
}
|
|
|
|
|
|
|
|
localStorage.token = response.data.token
|
|
|
|
window.location = 'dashboard'
|
2018-03-19 16:51:39 +00:00
|
|
|
}
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
page.onkeypress = (event, element) => {
|
2018-03-19 16:51:39 +00:00
|
|
|
event = event || window.event
|
2018-03-28 17:40:50 +00:00
|
|
|
if (!event) { return }
|
2018-04-29 12:47:24 +00:00
|
|
|
if (event.keyCode === 13 || event.which === 13) { page.do('login') }
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
page.verify = async () => {
|
|
|
|
if (!page.token) { return }
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-04-29 12:47:24 +00:00
|
|
|
const response = await axios.post('api/tokens/verify', {
|
2018-01-23 20:06:30 +00:00
|
|
|
token: page.token
|
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
.catch(error => {
|
|
|
|
console.log(error)
|
2018-04-29 12:47:24 +00:00
|
|
|
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-04-29 12:47:24 +00:00
|
|
|
if (!response) { return }
|
|
|
|
|
|
|
|
if (response.data.success === false) {
|
|
|
|
return swal('Error', response.data.description, 'error')
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location = 'dashboard'
|
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()
|
|
|
|
}
|