2023-05-08 21:31:47 +01:00
|
|
|
/* eslint-disable no-unused-vars */
|
2023-05-06 21:53:43 +01:00
|
|
|
/*
|
|
|
|
Auth process: User Requests Key => Server emails key to user =>
|
|
|
|
user opens link to auth.html =>
|
|
|
|
website POSTs key to server => Server checks validity =>
|
|
|
|
Server responds with the users auth key =>
|
|
|
|
auth.js adds this to localStorage
|
|
|
|
*/
|
|
|
|
const cmd = document.getElementById('cmd') // Assign element to const
|
|
|
|
versionDisplay() // Show web version in footer
|
|
|
|
init() // Run init function
|
|
|
|
|
|
|
|
async function sendHome(){
|
|
|
|
await delay(2000)
|
|
|
|
location.replace('./')
|
|
|
|
}
|
|
|
|
|
|
|
|
async function cmdOut(message) {
|
|
|
|
html = '<p>' + message + '</p>'
|
|
|
|
cmd.insertAdjacentHTML('beforeend', html)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function registerKey(key) { // Posts key to server and listens for response.
|
2023-07-01 22:17:07 +01:00
|
|
|
const url = `${apiEndpoint}/v1/register/register`
|
2023-05-06 21:53:43 +01:00
|
|
|
const res = await fetch(url, { // The response will contain the UUID which will be registered
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
redirect: 'follow',
|
|
|
|
body: JSON.stringify({uuid: key})
|
|
|
|
})
|
|
|
|
const data = await res.json()
|
|
|
|
return res.status === 201
|
|
|
|
? (localStorage.setItem('uuid', data.api_key), true)
|
|
|
|
: false
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkAuth(key) {
|
2023-07-01 22:17:07 +01:00
|
|
|
const url = `${apiEndpoint}/v1/auth/test`
|
2023-05-06 21:53:43 +01:00
|
|
|
const res = await fetch(url, {
|
|
|
|
method: 'GET',
|
|
|
|
redirect: 'follow',
|
|
|
|
headers: {
|
|
|
|
'uuid': key
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return res.status === 200 ? true : false
|
|
|
|
}
|
|
|
|
|
|
|
|
async function init(){ // Reads registration key from query, and calls registerKey(key)
|
|
|
|
cmdOut('Reading authorisation code')
|
|
|
|
const key = await getQuery('key')
|
|
|
|
if (key === 'false') {
|
|
|
|
cmdOut('No valid key found')
|
|
|
|
cmdOut('Try clicking the link again or request a new activation link')
|
|
|
|
hideLoading()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmdOut('Requesting API Key from server')
|
|
|
|
if (!await registerKey(key)) {
|
|
|
|
cmdOut('Failed to register or invalid key')
|
|
|
|
cmdOut('Try again later or request a new link')
|
|
|
|
hideLoading()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
showLoading()
|
|
|
|
if (! await checkAuth(localStorage.getItem('uuid'))) {
|
|
|
|
cmdOut('Authentication Check failed')
|
|
|
|
cmdOut('Please logout and request a new link')
|
|
|
|
hideLoading()
|
|
|
|
await delay(2000)
|
|
|
|
location.replace('./')
|
|
|
|
}
|
|
|
|
hideLoading()
|
|
|
|
cmdOut('Authentication succesful')
|
|
|
|
cmdOut('Redirecting to home')
|
|
|
|
await delay(3000)
|
|
|
|
location.replace('./')
|
|
|
|
}
|