diff --git a/packages/functions/src/https.ts b/packages/functions/src/https.ts index db92cad29..a6e510746 100644 --- a/packages/functions/src/https.ts +++ b/packages/functions/src/https.ts @@ -13,6 +13,7 @@ import { CustomerFull, sanitizeCustomer, DeliveryQueue, + checkExpected, } from "@eisbuk/shared"; import { checkRequiredFields, EisbukHttpsError } from "./utils"; @@ -147,7 +148,7 @@ export const customerSelfRegister = functions const orgDoc = await orgRef.get(); const orgData = orgDoc.data() as OrganizationData; - if (registrationCode !== orgData.registrationCode) { + if (!checkExpected(registrationCode, orgData.registrationCode || "")) { throw new EisbukHttpsError( "unauthenticated", HTTPSErrors.SelfRegInvalidCode, diff --git a/packages/shared/src/utils/__tests__/text.test.ts b/packages/shared/src/utils/__tests__/text.test.ts index e9a2dd73d..ce494a228 100644 --- a/packages/shared/src/utils/__tests__/text.test.ts +++ b/packages/shared/src/utils/__tests__/text.test.ts @@ -1,4 +1,4 @@ -import { interpolateText } from "../text"; +import { interpolateText, checkExpected } from "../text"; describe("Text utils", () => { describe("interpolateText", () => { @@ -35,5 +35,41 @@ describe("Text utils", () => { const interpolatedHtml = interpolateText(template, {}); expect(interpolatedHtml).toBe("Ciao"); }); + + test.only("Should ignore whitespace differences in registration code", async () => { + const cases = [ + ["A string", "A string!"], + ["Look, ma, some spaces!", "LOOKMASOMESPACES"], + ["Let's try with apostrophes", "Let s try with apostrophes."], + ["On the others side", "On the other's side"], + [ + "Nobody: use punctuation; You: expect it", + "Nobody? USE punctuation! You, expect it!", + ], + ]; + for (const [input, expected] of cases) { + const result = checkExpected(input, expected); + if (!result) { + console.log("input", input); + console.log("expected", expected); + } + expect(result).toEqual(true); + } + }); + + test.only("Should fail when strings are different", async () => { + const cases = [ + ["A string", "A string! You see?"], + ["Look, ma, some spaces!", "LOOKMANOSPACES"], + ]; + for (const [input, expected] of cases) { + const result = checkExpected(input, expected); + if (result) { + console.log("input", input); + console.log("expected", expected); + } + expect(result).toEqual(false); + } + }); }); }); diff --git a/packages/shared/src/utils/text.ts b/packages/shared/src/utils/text.ts index 72f2239c5..6bdac3a50 100644 --- a/packages/shared/src/utils/text.ts +++ b/packages/shared/src/utils/text.ts @@ -41,3 +41,13 @@ export const interpolateText = ( return template.trim(); }; + +export const checkExpected = (input: string, expected: string) => { + /* Compares two strings without taking into account differences + in whitespace, punctuation, and capitalization. + If the two strings are equal, returns true, otherwise false. + */ + const sanitizedInput = input.replace(/[\s,;:!?'.-]/g, "").toLowerCase(); + const sanitizedExpected = expected.replace(/[\s,;:!?'.-]/g, "").toLowerCase(); + return sanitizedInput === sanitizedExpected; +};