diff --git a/dapp/src/router/index.tsx b/dapp/src/router/index.tsx index 1a5b3ced0..351a8504e 100644 --- a/dapp/src/router/index.tsx +++ b/dapp/src/router/index.tsx @@ -38,9 +38,7 @@ export const router = createBrowserRouter([ // TODO: display the error page/modal when the referral is invalid. const referralCodeFromUrl = referralProgram.getReferralFromURL() - const referralCode = referralProgram.isValidReferral( - Number(referralCodeFromUrl), - ) + const referralCode = referralProgram.isValidReferral(referralCodeFromUrl) ? referralCodeFromUrl! : env.REFERRAL diff --git a/dapp/src/utils/referralProgram.ts b/dapp/src/utils/referralProgram.ts index 36f48b9a3..82a3bfbc5 100644 --- a/dapp/src/utils/referralProgram.ts +++ b/dapp/src/utils/referralProgram.ts @@ -9,9 +9,23 @@ const EMBEDDED_APP_TO_REFERRAL: Record = { "ledger-live": 2083, } -const isValidReferral = (value: number) => { - const isInteger = Number.isInteger(value) - return isInteger && value >= 0 && value <= MAX_UINT16 +const isValidReferral = (value: unknown) => { + let valueAsNumber: number | undefined + + if (typeof value === "string") { + // Only digits w/o leading zeros. + const isNumber = /^(?:[1-9][0-9]*|0)$/.test(value) + valueAsNumber = isNumber ? Number(value) : undefined + } else if (typeof value === "number") { + valueAsNumber = value + } + + return ( + !!valueAsNumber && + Number.isInteger(valueAsNumber) && + valueAsNumber >= 0 && + valueAsNumber <= MAX_UINT16 + ) } const getReferralFromURL = () =>