From 4f58aa8f9efeb6fab4a4435a5d980fc3a46d7b0a Mon Sep 17 00:00:00 2001 From: Abby Wheelis Date: Wed, 25 Oct 2023 14:12:32 -0600 Subject: [PATCH] re-work methods for checking code In a cleanup pass, we're working towards using URL methods rather than the "hacky" parts that I had before https://developer.mozilla.org/en-US/docs/Web/API/URL Also re-work the method to pull out the token from the url components and return the code if it's good, or false if the url is bad --- www/js/onboarding/WelcomePage.tsx | 34 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/www/js/onboarding/WelcomePage.tsx b/www/js/onboarding/WelcomePage.tsx index 5653218d7..cb317c5bc 100644 --- a/www/js/onboarding/WelcomePage.tsx +++ b/www/js/onboarding/WelcomePage.tsx @@ -20,28 +20,34 @@ const WelcomePage = () => { const [infoPopupVis, setInfoPopupVis] = useState(false); const [existingToken, setExistingToken] = useState(''); - const checkURL = function (result) { + const getCode = function (result) { + let url = new window.URL(result.text); let notCancelled = result.cancelled == false; let isQR = result.format == "QR_CODE"; - let hasPrefix = result.text.split(":")[0] == "emission"; - let hasToken = result.text.includes("login_token?token"); + let hasPrefix = url.protocol == "emission:"; + let hasToken = url.searchParams.has("token"); + let code = url.searchParams.get("token"); - logDebug("QR code " + result.text + " checks: cancel, format, prefix, params " + notCancelled + isQR + hasPrefix + hasToken); + logDebug("QR code " + result.text + " checks: cancel, format, prefix, params, code " + notCancelled + isQR + hasPrefix + hasToken + code); - return notCancelled && isQR && hasPrefix && hasToken; - } + if (notCancelled && isQR && hasPrefix && hasToken) { + return code; + } else { + return false; + } + }; - const scanCode = function() { + const scanCode = function () { window['cordova'].plugins.barcodeScanner.scan( function (result) { console.debug("scanned code", result); - if (checkURL(result)) { - let text = result.text.split("=")[1]; - console.log("found code", text); - loginWithToken(text); - } else { - displayError(result.text, "invalid study reference") ; - } + let code = getCode(result); + if (code != false) { + console.log("found code", code); + loginWithToken(code); + } else { + displayError(result.text, "invalid study reference"); + } }, function (error) { displayError(error, "Scanning failed: ");