filesafe/public/js/auth.js
Bobby Wibowo ee2ce394b1
Updates
* View thumbnail button will now use SweetAlert instead of Bulma's modal (preview: https://i.fiery.me/HDwX.png). It can be made more pretty but this will do for now.

* Fixed a bug where "Copy link to clipboard" button in albums list would only copy its identifier instead of the full URL.

Some other code improvements:

* All instances of adding/removing class names with Element.className will now use Element.classList. So instead of appending and replacing the string, it will now use add/remove functions, thus making it much easier to understand.

* "onkeypress" in auth page moved into a single addEventListener, sort of.

* Removed VSCode's discord extension entry from workspace settings. Eh, I'll go with user settings for this one.
2018-05-01 21:41:25 +07:00

68 lines
1.7 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.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()
document.body.addEventListener('keydown', event => {
event = event || window.event
if (!event) { return }
const id = event.target.id
if (!['user', 'pass'].includes(id)) { return }
if (event.keyCode === 13 || event.which === 13) { page.do('login') }
})
}