Backend (Fastify + TS): FluentForms webhook -> NocoDB row + QR + MailerSend email; PIN auth; scan/lookup/redeem with per-code serialization; reusable QR codes with count-based check-in; admin search. App (Expo, one codebase): Android APK + iPhone PWA. Login, camera scanner (native + web barcode-detector split), green/red overlay with sound + haptics, admin lookup/redeem. Session token persisted per device. Ops: multi-stage Dockerfile serving API + PWA same-origin, compose bound to 127.0.0.1; Forgejo Actions runner + tag-triggered signed APK build for Obtainium. Docs in README.md and INSTALL.md. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
120 lines
3.3 KiB
TypeScript
120 lines
3.3 KiB
TypeScript
import { Platform } from "react-native";
|
|
import { loadToken, saveToken, clearToken } from "./storage";
|
|
|
|
/**
|
|
* API base URL. On web the app is served from the same origin as the API, so we
|
|
* use a relative path. On native (the Android APK) it must point at the public
|
|
* HTTPS host, baked in at build time via EXPO_PUBLIC_API_URL.
|
|
*/
|
|
export const API_BASE =
|
|
Platform.OS === "web"
|
|
? ""
|
|
: (process.env.EXPO_PUBLIC_API_URL ?? "https://scan.beartariacampgrounds.com").replace(/\/+$/, "");
|
|
|
|
export interface TicketView {
|
|
code: string;
|
|
name: string;
|
|
email: string;
|
|
total: number;
|
|
redeemed: number;
|
|
remaining: number;
|
|
extras: {
|
|
carParking: boolean;
|
|
rvParking: boolean;
|
|
iceAccess: boolean;
|
|
isDonor: boolean;
|
|
freeUnder4: number;
|
|
};
|
|
ages: { bracket: string; count: number; free: boolean }[];
|
|
}
|
|
|
|
export class AuthError extends Error {}
|
|
export class ApiError extends Error {}
|
|
|
|
let cachedToken: string | null = null;
|
|
|
|
export async function getToken(): Promise<string | null> {
|
|
if (cachedToken) return cachedToken;
|
|
cachedToken = await loadToken();
|
|
return cachedToken;
|
|
}
|
|
|
|
export async function login(pin: string): Promise<void> {
|
|
const res = await fetch(`${API_BASE}/api/auth/login`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ pin }),
|
|
});
|
|
if (res.status === 401) throw new AuthError("Incorrect PIN");
|
|
if (!res.ok) throw new ApiError(`Login failed (${res.status})`);
|
|
const { token } = (await res.json()) as { token: string };
|
|
cachedToken = token;
|
|
await saveToken(token);
|
|
}
|
|
|
|
export async function logout(): Promise<void> {
|
|
cachedToken = null;
|
|
await clearToken();
|
|
}
|
|
|
|
async function authed<T>(path: string, init: RequestInit = {}): Promise<T> {
|
|
const token = await getToken();
|
|
if (!token) throw new AuthError("Not logged in");
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
|
...init,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${token}`,
|
|
...(init.headers || {}),
|
|
},
|
|
});
|
|
if (res.status === 401) {
|
|
await logout();
|
|
throw new AuthError("Session expired");
|
|
}
|
|
const text = await res.text();
|
|
let body: any = undefined;
|
|
if (text) {
|
|
try {
|
|
body = JSON.parse(text);
|
|
} catch {
|
|
body = text;
|
|
}
|
|
}
|
|
if (!res.ok) {
|
|
throw new ApiError(body?.error ?? body?.detail ?? `Request failed (${res.status})`);
|
|
}
|
|
return body as T;
|
|
}
|
|
|
|
export type LookupResult =
|
|
| { ok: true; found: true; ticket: TicketView }
|
|
| { ok: true; found: false }
|
|
| { ok: false; reason: "db_error"; detail: string };
|
|
|
|
export function lookup(code: string): Promise<LookupResult> {
|
|
return authed<LookupResult>("/api/lookup", {
|
|
method: "POST",
|
|
body: JSON.stringify({ code }),
|
|
});
|
|
}
|
|
|
|
export type RedeemResult =
|
|
| { ok: true; ticket: TicketView; checkedIn: number }
|
|
| {
|
|
ok: false;
|
|
reason: "not_found" | "exhausted" | "insufficient" | "db_error";
|
|
ticket?: TicketView;
|
|
detail?: string;
|
|
};
|
|
|
|
export function redeem(code: string, count: number): Promise<RedeemResult> {
|
|
return authed<RedeemResult>("/api/redeem", {
|
|
method: "POST",
|
|
body: JSON.stringify({ code, count }),
|
|
});
|
|
}
|
|
|
|
export function searchTickets(q: string): Promise<{ results: TicketView[] }> {
|
|
return authed<{ results: TicketView[] }>(`/api/tickets?q=${encodeURIComponent(q)}`);
|
|
}
|