From e3e98e8b9ecad0c6448ebdb5fdfbc9e3fb27d1d6 Mon Sep 17 00:00:00 2001 From: shockrah Date: Wed, 2 Feb 2022 22:57:36 -0800 Subject: [PATCH] !+ Auth verification flow now complete --- ts/admin.ts | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 ts/admin.ts diff --git a/ts/admin.ts b/ts/admin.ts new file mode 100644 index 0000000..11dd244 --- /dev/null +++ b/ts/admin.ts @@ -0,0 +1,71 @@ +// This module serves as convenience for admin users to upload/remove videos +// from their clippable instance. There are no fancy tricks as this is meant +// purely to be a UX thing. + +let UID: null|string = null +let KEY: null|string = null + +/* + * Requests to admin endpoints require the uid and api key be in the headers + * rather than the query string + */ +function setHeaders(xhr: XMLHttpRequest, uid?: string, key?: string) { + xhr.setRequestHeader('ADMIN-API-UID', uid ? uid : UID) + xhr.setRequestHeader('ADMIN-API-KEY', key ? key : KEY) +} + +function ok_response(xhr: XMLHttpRequest) { + return xhr.getResponseHeader('Content-Type') == 'application/json' && xhr.status == 200 +} + +function confirm_auth() { + let uid = document.getElementById('uid') + let key = document.getElementById('apikey') + + let xhr = new XMLHttpRequest() + + xhr.onload = function() { + if(ok_response(this)) { + document.getElementById('login-display').hidden = true + document.getElementById('dashboard').hidden = false + document.getElementById('error').hidden = true + } else if(this.status == 403) { + document.getElementById('error').hidden = false + document.getElementById('error').textContent = 'Invalid credenentials' + } else { + document.getElementById('error').hidden = false + document.getElementById('error').textContent = `${this.statusText}` + } + } + + xhr.open('post', window.location.origin + '/admin/dashboard') + setHeaders(xhr, (uid as HTMLTextAreaElement).value, (key as HTMLTextAreaElement).value) + + xhr.send() +} + + +function populate_meta_form() { + let file = document.getElementById('video-file') as HTMLInputElement + + // When we remove the file this array becomes 0 so the check is required + console.log('files found', file.files.length) + if(file.files.length == 0) { + document.getElementById('video-meta').hidden = true + } else { + // Size in MB + let size = file.files[0].size / 1024 / 1000 + document.getElementById('vmn').textContent = `${file.files[0].name}` + document.getElementById('vms').textContent = `${size} MB` + document.getElementById('vmt').textContent = `${file.files[0].type}` + + document.getElementById('video-meta').hidden = false + } +} + + + +document.addEventListener('DOMContentLoaded', () => { + document.getElementById('video-file').onchange = populate_meta_form + //document.getElementById('upload-video').onclick = upload_video +})