From 21f26dfa3833e5504f908c0d3647398c9fd8de90 Mon Sep 17 00:00:00 2001 From: Naviary <163621561+Naviary2@users.noreply.github.com> Date: Tue, 16 Jul 2024 01:08:38 -0600 Subject: [PATCH] createaccount script now automatically gets injected with our reserved usernames on startup, and saved. This prevents that page having to request this data after it loads. --- src/client/scripts/createaccount.js | 7 +- .../controllers/createaccountController.js | 1 - src/server/routes/createaccount.js | 6 +- src/server/utility/HTMLScriptInjector.js | 69 +++++++++++++++---- 4 files changed, 66 insertions(+), 17 deletions(-) diff --git a/src/client/scripts/createaccount.js b/src/client/scripts/createaccount.js index ac6d47e23..0bf785c03 100644 --- a/src/client/scripts/createaccount.js +++ b/src/client/scripts/createaccount.js @@ -1,10 +1,13 @@ +// THIS LINE WILL BE INJECTED by HTMLScriptInjector! +// const reservedUsernames = [...]; + const element_usernameInput = document.getElementById('username'); const element_emailInput = document.getElementById('email'); const element_passwordInput = document.getElementById('password'); const element_submitButton = document.getElementById('submit'); -// This will be an object with 3 arrays: memberList, reservedUsernames, profainWords +// This will be an object with 1 array: profainWords let data; fetch('/createaccount/data') .then((response) => response.json()) @@ -26,7 +29,7 @@ element_usernameInput.addEventListener('input', (event) => { // When username fi const formatError = !onlyLettersAndNumbers(element_usernameInput.value); // If data is still uninitiated (late fetch call), just assume there's no error. const usernameReservedError = - data ? !lengthError && data.reservedUsernames.indexOf(element_usernameInput.value.toLowerCase()) !== -1 + data ? !lengthError && reservedUsernames.includes(element_usernameInput.value.toLowerCase()) : false; const profainError = data ? !lengthError && checkProfanity(element_usernameInput.value) diff --git a/src/server/controllers/createaccountController.js b/src/server/controllers/createaccountController.js index 647d2e56e..1174bcb65 100644 --- a/src/server/controllers/createaccountController.js +++ b/src/server/controllers/createaccountController.js @@ -157,7 +157,6 @@ async function generateAccount({ username, email, password, autoVerify }) { // into the createaccount html instead. function getRegisterData(req, res) { res.json({ - reservedUsernames, profainWords }); } diff --git a/src/server/routes/createaccount.js b/src/server/routes/createaccount.js index e81564e6c..5adad8404 100644 --- a/src/server/routes/createaccount.js +++ b/src/server/routes/createaccount.js @@ -6,8 +6,12 @@ const path = require('path'); const createaccountController = require('../controllers/createaccountController') const {getRegisterData, checkEmailAssociated, checkUsernameAssociated} = require('../controllers/createaccountController'); +const createAccountHTMLPath = path.join(__dirname, '..', '..', '..', 'dist', 'views', 'createaccount.html'); + + + router.get('/', (req, res) => { - res.sendFile(path.join(__dirname, '..', '..', '..', 'dist', 'views', 'createaccount.html')); + res.sendFile(createAccountHTMLPath); }) router.post('/', createaccountController.createNewMember); diff --git a/src/server/utility/HTMLScriptInjector.js b/src/server/utility/HTMLScriptInjector.js index 1d91587af..2482a55dd 100644 --- a/src/server/utility/HTMLScriptInjector.js +++ b/src/server/utility/HTMLScriptInjector.js @@ -14,6 +14,7 @@ const fs = require('fs'); const path = require('path'); const glob = require('glob'); +const { getReservedUsernames } = require('../controllers/createaccountController'); /** * A cache object that has file paths for the keys, and for the values- @@ -26,14 +27,13 @@ let htmlCache = {}; * a specified tag, then cache's that content into {@link htmlCache} * @param {string} htmlFilePath - The path of the html document in the project * @param {string} jsFilePath - The path of the javascript file containing the desired javascript code to inject. - * @param {string} injectAfterTag - The HTML tag after which the JavaScript code will be injected (typically the `
`). * @param {Object} [stringInjection] - Optional argument: An object of the form {string: "htmlstring", injectafter: "tags"}. * The string will be insterted after the specified tags into the html doc */ -function prepareAndCacheHTML(htmlFilePath, jsFilePath, injectAfterTag, stringInjection = {}) { - injectScript(htmlFilePath, jsFilePath, injectAfterTag, stringInjection) +function prepareAndCacheHTML(htmlFilePath, jsFilePath, stringInjection = {}) { + injectScriptIntoHeadFromPaths(htmlFilePath, jsFilePath, stringInjection) .then(modifiedHTML => { - htmlCache[htmlFilePath] = modifiedHTML; + addHTMLToCache(htmlFilePath, modifiedHTML) }) .catch(error => console.error("Failed to inject script: ", error)); } @@ -41,14 +41,14 @@ function prepareAndCacheHTML(htmlFilePath, jsFilePath, injectAfterTag, stringInj /** * Injects a JavaScript file's content into an HTML file * after a specified tag, returning the new content. + * RECEIVES file paths, not raw data. * @param {string} htmlFilePath - The path of the html document in the project * @param {string} jsFilePath - The path of the javascript file containing the desired javascript code to inject. - * @param {string} injectAfterTag - The HTML tag after which the JavaScript code will be injected (typically the ``). * @param {Object} [stringInjection] - Optional argument: An object of the form {string: "htmlstring", injectafter: "tags"}. * The string will be insterted after the specified tags into the html doc * @returns {Promise