← Back to Projects

Engineering Case Study

GymTrix

A full-stack gym management platform built with Next.js, focused on dual-role authentication, automated subscription lifecycles, real-time biometric attendance tracking, and server-enforced business workflows.

Engineering Decisions

Key architectural and technical decisions made during the development of the project.

01

Dual-role authentication and session lifecycle

Manager dashboards and member portals maintain independent authentication lifecycles with distinct token sets and cookie scopes. Refresh tokens are stored in HttpOnly cookies, while Edge middleware verifies access without incurring database roundtrips.

02

Edge-level token renewal and route protection

Authentication verification and background token refreshes are handled in Edge middleware using lightweight Web Crypto standards (jose). This avoids database overhead on protected page requests while preventing session interruptions.

03

Decoupled biometric device integration

Biometric hardware interactions are isolated behind dedicated RESTful ingestion endpoints supporting both single check-ins and offline batch synchronizations, decoupling physical hardware constraints from internal business rules.

04

Server-enforced subscription state machine

Membership renewal and validation logic are strictly enforced on the server. Active subscriptions block premature renewal, preventing duplicate subscriptions and race conditions during concurrent access.

05

Multi-model workout builder architecture

Workout planning supports both structured relational data (days, exercises, progressive overload targets) and direct file attachments (PDF/images), providing flexibility for coaches while preserving clean database normalization.

Engineering Challenges

Real implementation challenges and the approaches used to solve them.

01

Preventing duplicate session deductions on multiple check-ins

Problem

In session-based memberships, athletes may swipe or scan their fingerprint multiple times within the same day, risking unauthorized extra session deductions.

Solution

Implemented an attendance validation pipeline that checks existing check-ins for the current calendar day in the local timezone (Asia/Tehran). Consecutive punches on the same date grant entry authorization while preserving remaining session credits.

02

Edge runtime compatibility with JWT verification

Problem

Standard Node.js cryptography libraries and ORM instances cause runtime failures and latency when executed inside Next.js Edge Middleware.

Solution

Built token verification around the Web Crypto-compliant jose library, keeping the middleware stateless and decoupled from the database while maintaining instant route protection.

03

Handling batch offline punch synchronization from hardware

Problem

Biometric devices operating intermittently or offline can dump large batches of attendance logs at once, which could lead to race conditions and inconsistent remaining session counters.

Solution

Created a dedicated batch sync endpoint (/api/attendance/sync) that processes punch logs sequentially within transactional boundaries, deduplicating records against existing daily attendances before mutating member balances.

04

Handling date-bounded vs counter-bounded subscription lifecycles

Problem

Gym memberships operate on two divergent models: fixed date intervals (daily / every other day) and count-based credits (session-based) that do not follow standard expiration timestamps.

Solution

Structured a polymorphic payment evaluation service that evaluates plan validity based on plan type—either comparing dates against server time or comparing usedSessions against totalSessions—ensuring consistent status reporting across both management and member views.