patterntypescriptnextjsModerate
Next.js App Router NFC scan flow with iron-session and drizzle-orm
Viewed 1 times
Next.js 15+, drizzle-orm 0.45+
scan flowNFC chipcompliance trackingtimezone dateredirect chaindynamic route params
nodejsssrmobile
Problem
Building an NFC chip scan flow in Next.js App Router requires coordinating dynamic route params, iron-session auth, drizzle-orm queries, timezone-aware date handling, and proper redirect chains for success/error states.
Solution
Use a GET route handler at /scan/[chipId]/route.ts. Await params (Next.js 15+ requires
await params). Validate chip existence, active status, and worker ownership. Use Intl.DateTimeFormat('en-CA', { timeZone }) for YYYY-MM-DD dates. Check for duplicate scans with composite (workerId, equipmentTypeId, scanDate) query. Redirect to /worker?error=... for errors, /worker/scan-success?type=... for success. Worker dashboard is a server component that queries scans and renders compliance status.Why
NFC chips open URLs when tapped, so the scan handler must be a GET endpoint that validates auth, chip ownership, deduplicates, inserts, and redirects - all in one request.
Gotchas
- In Next.js 15+, route handler params must be awaited: const { chipId } = await params
- en-CA locale with Intl.DateTimeFormat gives YYYY-MM-DD format needed for date comparisons
- Scan deduplication must use all 3 fields (workerId + equipmentTypeId + scanDate) not just 2
Code Snippets
Timezone-aware YYYY-MM-DD date using en-CA locale trick
export function getTodayDate(): string {
const tz = process.env.SITE_TIMEZONE || "UTC";
return new Intl.DateTimeFormat("en-CA", { timeZone: tz }).format(new Date());
}Context
Building NFC-based compliance tracking where physical chip taps trigger URL opens on mobile devices
Revisions (0)
No revisions yet.