Add offline ticket scanning
Staff can lock a section (or the whole event) to their device, download its tickets and keep scanning with no connection. Scans are queued in IndexedDB and synced idempotently when back online; conflicts go into a sync report for supervisors, who can also force-release a lost device. The server now validates scan sections and refuses scans in a scope locked to another device. Adds a web app manifest and a service worker for the scanner page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,25 @@ export type AuthContextValue = {
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | undefined>(undefined);
|
||||
|
||||
// The last profile the server confirmed, so a page reloaded with no connection (e.g. the
|
||||
// ticket scanner in offline mode) still knows who's signed in. The server checks the
|
||||
// token on every API call regardless, so this never grants anything by itself.
|
||||
const USER_CACHE_KEY = "hope_events_user_cache";
|
||||
function cacheUser(u: AuthUser | null) {
|
||||
try {
|
||||
if (u) localStorage.setItem(USER_CACHE_KEY, JSON.stringify(u));
|
||||
else localStorage.removeItem(USER_CACHE_KEY);
|
||||
} catch { /* storage unavailable */ }
|
||||
}
|
||||
function cachedUser(): AuthUser | null {
|
||||
try {
|
||||
const raw = localStorage.getItem(USER_CACHE_KEY);
|
||||
return raw ? JSON.parse(raw) : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const [token, setToken] = useState<string | null>(null);
|
||||
const [user, setUser] = useState<AuthUser | null>(null);
|
||||
@@ -34,6 +53,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
const me = await apiFetch<AuthUser>("/api/users/profile", { authToken: t });
|
||||
setToken(t);
|
||||
setUser(me);
|
||||
cacheUser(me);
|
||||
}
|
||||
} catch (e) {
|
||||
// Only wipe the stored token when the server explicitly rejects it (401).
|
||||
@@ -41,8 +61,17 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
// token stays in localStorage so the next page load can retry.
|
||||
if (e instanceof ApiError && e.status === 401) {
|
||||
clearToken();
|
||||
cacheUser(null);
|
||||
setToken(null);
|
||||
setUser(null);
|
||||
} else {
|
||||
// No connection: carry on as the last confirmed user.
|
||||
const t = getToken();
|
||||
const cached = cachedUser();
|
||||
if (t && cached) {
|
||||
setToken(t);
|
||||
setUser(cached);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
setLoading(false);
|
||||
@@ -57,6 +86,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
saveToken(token);
|
||||
setToken(token);
|
||||
setUser(loggedInUser);
|
||||
cacheUser(loggedInUser);
|
||||
return loggedInUser;
|
||||
}, []);
|
||||
|
||||
@@ -99,6 +129,7 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
|
||||
const logout = useCallback(() => {
|
||||
clearToken();
|
||||
cacheUser(null);
|
||||
setToken(null);
|
||||
setUser(null);
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user