ADR-00041 — Mobile Login & Signup: Phone-OTP, Dual-Path Initialization, and Consent Design
- Status: Accepted
- Date: 2026-10-10
- Authors: Engineering Team
Context
BRD-026 requires phone-number-only account creation and login, confirmed by a one-time code, with mandatory Terms/Privacy acceptance at signup and no manual step to get a new account ready to use. SR-027 turns that into concrete endpoint and saga behavior. Several points in that behavior had more than one workable shape, and the choices made are not obvious from the code alone — this record captures why each one was made.
Decision Drivers (FRs & NFRs)
- FR-01/FR-02 (login send/verify), FR-03/FR-04 (signup send/verify) — existence-precondition checks before any code is sent.
- FR-06 (single consent checkbox, two tracked document versions).
- FR-07 (dual-path, idempotent account provisioning).
- NFR-02 (OTP codes bound to full request context, never logged in production).
- NFR-03 (no password credential, ever).
- NFR-06 (saga compensation on partial failure).
Decision 1 — OTP mechanism: custom server-side codes vs. Firebase-native phone/email verification
Option 1 — Firebase-native phone authentication (client-side reCAPTCHA/SMS flow) and Firebase-native email verification links
- Pros:
- No OTP storage, hashing, or rate-limiting code to write or operate; Firebase handles SMS delivery/retry for phone, and the verification email/link for email.
- Cons (Why Rejected):
- Firebase’s phone-auth flow is a client-SDK-driven flow built around
RecaptchaVerifier/ConfirmationResultobjects live in the client session — it has no server-side hook to reject a signup against an already-existing phone number, or a login against a non-existent one, before a code is sent (BRD-026’s BR-05 / SR-027’s FR-01/FR-03). - Firebase bills phone-auth SMS delivery at its own per-message rate, which runs materially higher than a direct carrier/SMS-gateway rate for Indian phone numbers at this platform’s expected volume — a recurring operating cost, not a one-time integration cost, and one this decision weighed directly.
- Firebase’s native email verification is a link-based flow: the link is emailed to the shopper’s inbox and only completes correctly if opened on the same device/session that started the signup. A shopper who signs up on a mobile device but reads email on a different device or desktop client — common in practice — opens a link with no originating app session to redirect back into, so the verification can’t finish the flow it started.
- Enforcing the existence precondition on top of either flow would require a server-side lookup bolted in front of a flow that wasn’t designed for it either way, while still leaving delivery timing, retry semantics, and billing outside this codebase’s control.
- Firebase’s phone-auth flow is a client-SDK-driven flow built around
Option 2 — Custom server-side 6-digit OTP, hashed and rate-limited in user-secure-engine, shared across phone and email channels (CHOSEN)
- Pros:
user-secure-enginecan run the existence/non-existence precondition as the very first step ofsend, before any code is generated or any SMS cost is incurred (NFR-02’s rate-limit gate also sits at this same chokepoint).- The stored value is an HMAC-SHA256 hash computed over the full request context — tenant, purpose, channel, contact — not the bare code, so a verify call can only match the exact request it was generated against, and the plaintext code itself never needs to touch a durable log at production level.
- One mechanism serves both contact channels, not two:
OtpChannel::SmsandOtpChannel::Emailshare the samehash_code/send/verifypath, the sameotp_codescollection, and the same cooldown/rate-limit bookkeeping (domain/otp/otp.rs,service/otp/otp_svc_impl.rs) — adding email verification needed a new enum variant, not a second verification system. The same path already backs the Settings feature’s email/phone-change flow (OtpPurpose::ProfileUpdate→commit_email_change/commit_phone_change), so this isn’t a phone-only mechanism with email bolted on afterward. - A typed code entered back into the same app session has no device-mismatch failure mode — it sidesteps the email-link redirect problem in Option 1 entirely, for both channels.
- Accepted Trade-offs:
- This codebase owns OTP generation, hashing, cooldown/rate-limit bookkeeping, resend-attempt capping, and the actual SMS/email delivery integration itself, rather than delegating that surface to Firebase.
Decision 2 — Separate send/verify endpoint pairs per purpose vs. one shared pair
Option 1 — One shared /auth/otp/send + /auth/otp/verify, with an internal “login or signup” branch
- Cons (Why Rejected):
- Login and signup have opposite existence-precondition polarity (login requires the account to already exist; signup requires it not to) and divergent post-verify behavior (login only mints a token; signup additionally requires consent, creates the Firebase account, and starts the initialization saga).
- Collapsing both into one endpoint pushes that branching into the handler body and makes the two purposes share one OTP hash/rate-limit bucket per phone number, which would let a signup attempt and a login attempt on the same number interfere with each other’s rate limit.
Option 2 — Separate login/signup endpoint pairs, keyed by distinct OtpPurpose values (CHOSEN)
- Pros:
- Each purpose gets its own hash bucket (
OtpPurpose::PhoneLoginvs.OtpPurpose::PhoneSignup), so a shopper mid-signup and a shopper mid-login on the same phone number never collide. - Each handler states its own precondition polarity directly, rather than branching on a flag.
- Each purpose gets its own hash bucket (
- Accepted Trade-offs:
- Four endpoints instead of two; the signup/login mechanics documented separately (SR-027 FR-01 through FR-04) rather than as one combined flow.
Decision 3 — Account initialization: dual synchronous call + onCreate trigger vs. a single path
Option 1 — Rely solely on the Firebase onCreate background trigger
- Cons (Why Rejected):
- The trigger fires from a
UserRecordsnapshot taken at account-creation time.auth_svc_impl.rs’sverify_signup_otpsets custom claims (terms_accepted,terms_version,privacy_version) after the Firebase account is created — the snapshot the trigger receives is captured strictly before those claims land, so a trigger-only path would always read consent as unset at the moment it builds its initialization request.
- The trigger fires from a
Option 2 — Rely solely on the synchronous call inside verify_signup_otp
- Cons (Why Rejected):
- This leaves no safety net if the synchronous call’s connection drops after the Firebase account is already created (account creation and local initialization are not one atomic operation) — a shopper could end up with a working Firebase identity and no
usersrow, with nothing to retry it.
- This leaves no safety net if the synchronous call’s connection drops after the Firebase account is already created (account creation and local initialization are not one atomic operation) — a shopper could end up with a working Firebase identity and no
Option 3 — Both: synchronous call as source of truth, trigger as idempotent safety net (CHOSEN)
- Pros:
- The synchronous call inside
verify_signup_otpruns after custom claims are set, so it always has the real consent values and is the actual source of truth for theusersrow. - The Firebase v1
onCreatetrigger (v2 has no non-blocking background equivalent for account creation — onlybeforeUserCreated/beforeUserSignedInblocking functions exist, which solve a different problem) calls the sameinit_useroperation independently; becauseinit_userstarts with afind_by_idexistence check, the trigger’s later call becomes a harmless no-op in the normal case, and becomes the actual safety net in the rare case where the synchronous call never completed. - The trigger’s call is itself a synchronous, deadline-bound gRPC call from
firebase-functionintouser-secure-engine’sValidateAndInitUserRPC (grpc/user-service-client.js), carryingtenant_id/user_id/roleas gRPC metadata like any other internal gRPC caller — the mobile client is never involved in this second path and never calls a Firebase Admin API itself. The resilience this decision buys comes from two independent backend callers ofinit_user, not from giving the client a second route to Firebase.
- The synchronous call inside
- Accepted Trade-offs:
- Two code paths calling the same operation is less obvious at a glance than a single path, and relies on
init_user’s idempotency being preserved by any future change to that method.
- Two code paths calling the same operation is less obvious at a glance than a single path, and relies on
Decision 4 — User + PII creation: compensating saga vs. distributed transaction
Option 1 — Distributed transaction spanning the identity store and pii-service
- Cons (Why Rejected):
pii-serviceis a separate gRPC-only microservice with its own datastore; no two-phase-commit coordinator exists across it anduser-secure-engine’s own store, and introducing one for a single call sequence is disproportionate to the actual failure mode being guarded against.
Option 2 — PII write, then identity insert, with a compensating delete on failure (CHOSEN)
- Pros:
init_userupserts PII attributes first, then attempts the identity-row insert; if that insert fails, it issues a compensating delete against the just-written PII rather than leaving an orphaned record with no owning account (NFR-06).- This fits the EAV attribute-store shape
pii-servicealready exposes, with no new coordination infrastructure.
- Accepted Trade-offs:
- A failure between the compensating delete’s own call and its completion is not itself further compensated — accepted as a residual, low-probability risk rather than a system requiring full saga-orchestration tooling for one two-step sequence.
Decision 5 — Consent capture: one checkbox vs. two
Option 1 — Two separate checkboxes/claims, one per document
- Cons (Why Rejected):
- BRD-026 treats Terms and Privacy acceptance as a single act of consent at signup (BR-03) — splitting it into two independent claims would add a second piece of UI state and a second custom claim with no requirement driving the split, and no part of the current policy-viewer or claims model distinguishes “accepted Terms but not Privacy” as a reachable state.
Option 2 — Single terms_accepted claim, with terms_version and privacy_version still tracked independently (CHOSEN)
- Pros:
- Matches the actual shopper-facing action (one checkbox, reachable after reviewing both documents via the three-tab policy viewer).
- The two version identifiers remain genuinely independent fields on the custom claims and on the
InitUserInputDTO, so a future change to only one document’s version is still distinguishable from the other, without requiring two acceptance flags to do it.
- Accepted Trade-offs:
- If a future requirement ever needs independently revocable Terms vs. Privacy consent, this claim shape would need to be revisited — not a concern this phase’s requirements raise.
Final Decision
Custom server-owned OTP (Decision 1, Option 2), four purpose-scoped endpoints (Decision 2, Option 2), dual synchronous-call-plus-trigger initialization with the synchronous call as source of truth (Decision 3, Option 3), a compensating-delete saga over a distributed transaction (Decision 4, Option 2), and a single consent claim over two independently versioned documents (Decision 5, Option 2).
Related Documents
- BRD-026 — Mobile Login & Signup: Phone-Based Identity and Legal Acceptance
- SR-027 — Mobile Login & Signup: System Requirements
- Architecture — Mobile Login & Signup: Component Topology and Data Flow
- Technical Debt — Mobile Login & Signup Gaps
- Microservices: user-secure-engine, pii-service, firebase-function