ContactHub Backend Service
Full-stack contact management engine live at contact-manager-fawn-alpha.vercel.app — Go clean architecture, bcrypt crypto, and partial unique indexing.
Missing, deleted and unauthorised records answer identically, so contact IDs cannot be enumerated
Background worker purges 1,000 rows per batch, with graceful shutdown
A password reset invalidates every outstanding JWT without a denylist
Case-insensitive substring search, LIKE metacharacters escaped
A contact API that holds several users' data has to be wrong only once to leak it. Scoping queries by user is the easy half; the hard half is that a 404 for a record that does not exist, one that was deleted, and one that belongs to somebody else all have to be indistinguishable, or the IDs can be walked. Authentication adds its own: a login that answers faster for unknown emails than for real ones tells an attacker which addresses are registered.
- 01Chi router v5Transport
JSON decoding, route validation and a single error envelope shape.
Validation at the edge - 02Service layerBusiness
bcrypt verification, JWT issue carrying token_version, domain rules.
Timing-safe on unknown emails - 03Repository (SQL)Persistence
Parameterised queries, every one scoped WHERE user_id = $1.
Tenant isolation - 04PostgreSQLDatabase
Partial unique index on (user_id, phone) WHERE deleted_at IS NULL.
Soft delete stays restorable
Timing-Safe Account Enumeration Defence
An unknown email still pays the cost of a bcrypt comparison against a dummy hash, so response time cannot be used to discover which accounts exist.
Partial Unique Index on Soft Delete
Phone uniqueness is scoped per user and to live rows only, so a deleted contact can be recreated or restored without colliding with its own tombstone.
Instant Session Revocation Without Redis
A token_version column in PostgreSQL is carried in the JWT claims; a password reset increments it and every previously issued token stops validating.
A dummy bcrypt verification for emails that do not exist
An unknown email normally returns 401 in about 2ms while a real one pays roughly 70ms for the hash comparison, and that gap is a free list of who has an account. Running a bcrypt comparison against a pre-computed hash for unknown users makes the two paths take the same time.
A partial unique index scoped to live rows, not a table-wide constraint
A global UNIQUE on phone would stop two different users saving the same number, and stop anyone restoring a contact they had deleted. A composite partial index on (user_id, phone) WHERE deleted_at IS NULL scopes uniqueness to one user and to records that are actually active.
A token_version column instead of a session store
Revoking a session instantly usually means keeping state somewhere — a Redis denylist, and a new dependency to run and fail. Carrying a version number in the JWT claim and bumping the column on password change invalidates every outstanding token at once, with the database already there.
The rate limiter only works because there is one container. internal/ratelimit holds its per-IP and per-account counters in process, so they reset on every restart and do not hold across replicas — put two instances behind a load balancer and each one happily enforces its own separate budget, which is to say neither enforces the limit. It is the right amount of machinery for a single box and the wrong amount the moment there are two. The shortener solves the same problem with an atomic Redis Lua script over a sorted set; that is where this one should go.