filesafe/public/js/auth.js
Bobby Wibowo 61e1896945
Updates
* A bunch of refactors in public JS files (home.js, dashboard.js, etcetera).

* Added lazyload to home page (for thumbs of uploaded images), dashboard (for thumbs view) and albums' public link.
Albums' public link will silently fallback to loading all thumbs at once if JavaScript is disabled.

* A bunch of others code improvements. Honestly I'm too lazy to track all the changes.
2018-04-29 19:47:24 +07:00

66 lines
1.6 KiB
JavaScript

/* global swal, axios */
const page = {
// user token
token: localStorage.token
}
page.do = async dest => {
const user = document.getElementById('user').value
const pass = document.getElementById('pass').value
if (!user) {
return swal('Error', 'You need to specify a username', 'error')
}
if (!pass) {
return swal('Error', 'You need to specify a username', 'error')
}
const response = await axios.post(`api/${dest}`, {
username: user,
password: pass
})
.catch(error => {
console.log(error)
return swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
if (!response) { return }
if (response.data.success === false) {
return swal('Error', response.data.description, 'error')
}
localStorage.token = response.data.token
window.location = 'dashboard'
}
page.onkeypress = (event, element) => {
event = event || window.event
if (!event) { return }
if (event.keyCode === 13 || event.which === 13) { page.do('login') }
}
page.verify = async () => {
if (!page.token) { return }
const response = await axios.post('api/tokens/verify', {
token: page.token
})
.catch(error => {
console.log(error)
swal('An error occurred!', 'There was an error with the request, please check the console for more information.', 'error')
})
if (!response) { return }
if (response.data.success === false) {
return swal('Error', response.data.description, 'error')
}
window.location = 'dashboard'
}
window.onload = () => {
page.verify()
}