Files
co-lite/solvway-customer-portal/app/lib/referral.ts
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

38 lines
1.7 KiB
TypeScript

// Client-safe persistence of the referral code that arrives on the referral
// link (/register?ref=<memberCode>). It is stashed in a short-lived cookie so
// it survives a bounce to /login and the login→register navigation, instead of
// living only in the register page's URL where any redirect would drop it.
export const REF_COOKIE = "te_ref";
const MAX_AGE_SECONDS = 30 * 60; // 30 minutes — long enough to register, short enough to not linger.
/** The `ref` query param, trimmed; from an explicit search string, or the live URL. "" when absent. */
export function refFromQuery(search?: string): string {
const qs = search ?? (typeof window !== "undefined" ? window.location.search : "");
return new URLSearchParams(qs).get("ref")?.trim() || "";
}
/** Persist a referral code in a short-lived cookie. No-op for empty values or non-browser contexts. */
export function rememberRef(code: string): void {
if (typeof document === "undefined" || !code) return;
document.cookie = `${REF_COOKIE}=${encodeURIComponent(code)}; path=/; max-age=${MAX_AGE_SECONDS}; samesite=lax`;
}
/** The stored referral code, or "" if none. */
export function getStoredRef(): string {
if (typeof document === "undefined") return "";
const match = document.cookie.match(new RegExp(`(?:^|; )${REF_COOKIE}=([^;]*)`));
return match ? decodeURIComponent(match[1]) : "";
}
/** Clear the stored referral code. */
export function clearStoredRef(): void {
if (typeof document === "undefined") return;
document.cookie = `${REF_COOKIE}=; path=/; max-age=0; samesite=lax`;
}
/** The effective referral code: the URL's `ref` takes precedence over the stored one. */
export function resolveRef(): string {
return refFromQuery() || getStoredRef();
}