filesafe/public/js/auth.js
Bobby Wibowo 3a398721b5
Updated
* Replaced all instances of getElementById and getElementsByClassName
with querySelector or querySelectorAll.

* Updated utilsController.js to stop disabling
no-async-promise-executor eslint rule.

* Removed unused lines in dashboard.njk.

* Refactored maxFileSize to maxSize in home.{css,js,njk}.

* Updated ClamAV codes in lolisafe.js. No more pinging.
Since querying version will also check connection anyway.

* Option "Upload to album" in homepage is now selectable.
Selecting this option will restore the uploader to not associate files
with an album.

* Fixed uploader to properly respect server's max file size.
Also updated error message of file size to use MB instead of MiB.

* Creating an album from homepage will automatically select the album.

* Updated Dropzone.js to v5.5.0.

* Bumped v1 & v3 version strings.

* Various other small fixes.
2019-08-20 09:16:34 +07:00

75 lines
2.0 KiB
JavaScript

/* global swal, axios */
const page = {
// user token
token: localStorage.token,
// HTML elements
user: null,
pass: null
}
page.do = function (dest) {
const user = page.user.value
const pass = page.pass.value
if (!user)
return swal('An error occurred!', 'You need to specify a username.', 'error')
if (!pass)
return swal('An error occurred!', 'You need to specify a password.', 'error')
axios.post(`api/${dest}`, {
username: user,
password: pass
}).then(function (response) {
if (response.data.success === false)
return swal(`Unable to ${dest}!`, response.data.description, 'error')
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')
})
}
page.verify = function () {
if (!page.token) return
axios.post('api/tokens/verify', {
token: page.token
}).then(function (response) {
if (response.data.success === false)
return swal('An error occurred!', response.data.description, 'error')
window.location = 'dashboard'
}).catch(function (error) {
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')
})
}
window.onload = function () {
page.verify()
page.user = document.querySelector('#user')
page.pass = document.querySelector('#pass')
// Prevent default form's submit action
document.querySelector('#authForm').addEventListener('submit', function (event) {
event.preventDefault()
})
document.querySelector('#loginBtn').addEventListener('click', function () {
page.do('login')
})
document.querySelector('#registerBtn').addEventListener('click', function () {
page.do('register')
})
}