filesafe/src/js/auth.js
Bobby Wibowo c3d61733af
Updated
Added iamdustan/smoothscroll polyfill in dashboard pages.
This will polyfill smooth scroll (when executed programmatically)
for older browers.

No-JS uploader's notice button when on private mode will now also say
"Log in to upload", although auth page will still require JS.

All front-end buttons will now use outlined version. I'm lovin' it.

Auth page will now show a loading spinner if the user has a saved token.
Afterwards, they will still be redirected to dashboard.

Better error handlers in home, dashboard, and auth pages.

Removed <hr> from uploads & users lists in dashboard.

"Manage your token" menu will no longer try to make an API request prior
to displaying its page.
Reloading the page will already trigger token verification anyway.

Updated public/images/fb_share.png.

Updated README.md.

A few other tweaks.
2019-09-19 14:19:11 +07:00

115 lines
2.9 KiB
JavaScript

/* global swal, axios */
const lsKeys = {
token: 'token'
}
const page = {
// user token
token: localStorage[lsKeys.token],
// HTML elements
user: null,
pass: null
}
page.unhide = () => {
document.querySelector('#loader').classList.add('is-hidden')
document.querySelector('#login').classList.remove('is-hidden')
}
// Handler for Axios errors
page.onAxiosError = error => {
console.error(error)
// Better Cloudflare errors
const cloudflareErrors = {
520: 'Unknown Error',
521: 'Web Server Is Down',
522: 'Connection Timed Out',
523: 'Origin Is Unreachable',
524: 'A Timeout Occurred',
525: 'SSL Handshake Failed',
526: 'Invalid SSL Certificate',
527: 'Railgun Error',
530: 'Origin DNS Error'
}
const statusText = cloudflareErrors[error.response.status] || error.response.statusText
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} ${statusText}`, description, 'error')
}
page.do = (dest, trigger) => {
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')
trigger.classList.add('is-loading')
axios.post(`api/${dest}`, {
username: user,
password: pass
}).then(response => {
if (response.data.success === false) {
trigger.classList.remove('is-loading')
return swal(`Unable to ${dest}!`, response.data.description, 'error')
}
localStorage.token = response.data.token
window.location = 'dashboard'
}).catch(error => {
trigger.classList.remove('is-loading')
page.onAxiosError(error)
})
}
page.verify = () => {
axios.post('api/tokens/verify', {
token: page.token
}).then(response => {
if (response.data.success === false) {
page.unhide()
return swal('An error occurred!', response.data.description, 'error')
}
// Redirect to dashboard if token is valid
window.location = 'dashboard'
}).catch(error => {
page.unhide()
page.onAxiosError(error)
})
}
window.onload = () => {
page.user = document.querySelector('#user')
page.pass = document.querySelector('#pass')
// Prevent default form's submit action
const form = document.querySelector('#authForm')
form.addEventListener('submit', event => {
event.preventDefault()
})
document.querySelector('#loginBtn').addEventListener('click', event => {
if (!form.checkValidity()) return
page.do('login', event.currentTarget)
})
document.querySelector('#registerBtn').addEventListener('click', event => {
if (!form.checkValidity()) return
page.do('register', event.currentTarget)
})
if (page.token)
page.verify()
else
page.unhide()
}