filesafe/public/js/auth.js
Bobby Wibowo 02e2e402c3
!!! MASSIVE OVERHAUL !!!
As the title says, this commit is a massive overhaul.
I've rewritten/restrucuted almost everything in the controller scripts.
Because of that, there's a considerable possibility that I've broken
something somewhere.

Notable changes:

Added temporary uploads.

Removed file name length changer from dashboard,
in favor of an equivalent in homepage config tab.
This allows non-registered users to also set file name length.

A bunch of other undocmented stuff.
I don't know, I'm too tired to remember them all.
2019-09-08 08:56:29 +07:00

78 lines
2.1 KiB
JavaScript

/* global swal, axios */
const lsKeys = {
token: 'token'
}
const page = {
// user token
token: localStorage[lsKeys.token],
// HTML elements
user: null,
pass: null
}
page.do = function (dest) {
const user = page.user.value.trim()
if (!user)
return swal('An error occurred!', 'You need to specify a username.', 'error')
const pass = page.pass.value.trim()
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.error(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')
})
}