Mobile Login & Signup: Component Topology and Data Flow
By Engineering Team•October 10, 2026•7 min read
| Metadata | Details |
|---|---|
| System Name | Mobile Login & Signup — ui-application’s pre_auth feature + user-secure-engine |
| Document ID | ARCH-2026-AUTH-001 |
| Author | Engineering Team |
| Status | Implemented (gaps noted inline) |
Realizes SR-027’s requirements under the decisions recorded in ADR-00041.
Diagram

Components
| Component | Responsibility |
|---|---|
ui-application’s pre_auth feature (splash, phone signup, login, OTP verify, policy viewer) |
Flutter screens and GetX controllers driving the shopper-facing identity flow; owns no persistent state of its own beyond the active session. |
phone_auth_repository.dart |
Sole client-side caller of user-secure-engine’s four /auth/otp/* endpoints; funnels every successful verify through a shared _completeSignIn (signInWithCustomToken → saveUserSession). |
policy_viewer_controller.dart |
Fetches Terms/Privacy/Returns document content and the currently-in-effect version identifiers for display and for stamping onto the signup consent action. |
CDN edge / nginx-api-router |
Public entry point; for the four pre-auth OTP routes, enforces Firebase App Check only (/_app_check_verify), since no bearer token exists yet. |
user-secure-engine’s AuthService (auth_svc_impl.rs) |
Orchestrates OTP send/verify for both login and signup; builds synthetic AuthClaims (preauth_claims, firebase_uid_claims) to drive OTP hashing/audit and the post-signup init call. |
user-secure-engine’s OTP store (otp_codes Mongo collection) |
HMAC-SHA256-hashed codes keyed by (tenant_id, channel, contact, purpose); backs rate limiting and verify-attempt capping. |
| Firebase Identity Toolkit | Source of truth for the Firebase account itself (phone-number uniqueness backstop, custom-token minting, custom claims storage). |
user-secure-engine’s UserService (init_user) |
Idempotent account-provisioning operation: existence check, PII upsert, identity-row insert, compensating delete on failure. |
pii-service |
Encrypted PII attribute store (full_name, email, phone_number) under entity type user_personal, gRPC-only. |
firebase-function’s user_account_created trigger |
Firebase v1 onCreate background trigger; independently calls the same init_user operation as a safety net, reading consent/version data off the account’s custom claims. |
End-to-end flow (A) — First launch and identity choice
- The app opens to a splash screen while
ui-applicationinitializes (Firebase SDK, App Check activation, session check). - The shopper is presented the choice to sign up or log in — no identity choice is made for them, and no account check happens before this point.
End-to-end flow (B) — Signup: policy review, consent, OTP send/verify, account creation
- The shopper enters a phone number on the signup screen (
phone_signup_controller.dart) and may open the policy viewer to review Terms & Conditions, Privacy Policy, and Returns & Exchange (policy_viewer_controller.dart’s three tabs) before returning to check the single consent box (SR-027 FR-05/FR-06). - On submit,
phone_auth_repository.dartcallsPOST /auth/otp/signup/send. The gateway verifies App Check only (/_app_check_verify, no bearer token) before forwarding touser-secure-engine(SR-027 FR-09). AuthService::send_signup_otplooks up the phone number against Firebase Identity Toolkit; if an account already exists, the call is rejected (409) before any code is generated (SR-027 FR-03). Otherwise a 6-digit code is generated, hashed underOtpPurpose::PhoneSignup, and sent, subject to cooldown/hourly rate limiting (SR-027 NFR-01/NFR-02).- The shopper enters the code on the OTP-verify screen (
otp_verify_controller.dart, signup mode);phone_auth_repository.dartcallsPOST /auth/otp/signup/verifywith the code and the consent flag/version identifiers captured in step 1. AuthService::verify_signup_otprejects immediately if the consent flag is false (SR-027 FR-04). On a successful code match, it callscreate_user_by_phone— Identity Toolkit itself is the atomic backstop against a concurrent duplicate signup for the same number, mapped to a conflict error if it loses the race.- Still inside the same request,
verify_signup_otpsets custom claims (role,terms_accepted,terms_version,privacy_version) on the new Firebase account, then synchronously callsUserService::init_userwith those same values — this is the authoritative path for theusersrow and PII attributes (flow D), run before the trigger in flow C can possibly fire with correct data. verify_signup_otpmints a Firebase custom token and returns it to the client, which completes sign-in viasignInWithCustomToken→saveUserSession(shared_completeSignIn).
End-to-end flow (C) — Login: existence check, OTP send/verify, no writes
- The shopper enters a phone number on the login screen (
login_single_controller.dart);phone_auth_repository.dartcallsPOST /auth/otp/login/send, gated by the same App-Check-only enforcement as signup. AuthService::send_login_otplooks up the phone number; if no account exists, the call is rejected (404) before any code is sent (SR-027 FR-01). Otherwise a code is generated and sent underOtpPurpose::PhoneLogin— a distinct hash/rate-limit bucket from signup, so a shopper mid-signup and mid-login on the same number never collide (ADR-00041, Decision 2).- The shopper enters the code (
otp_verify_controller.dart, login mode);phone_auth_repository.dartcallsPOST /auth/otp/login/verify. AuthService::verify_login_otpverifies the code, re-looks-up the phone number for its Firebase uid, and mints a custom token directly — no Identity Toolkit write of any kind happens on login (SR-027 FR-02).- The client completes sign-in via the same shared
_completeSignInas signup.
End-to-end flow (D) — Dual-path account initialization saga
- Synchronous path (source of truth): Inside
verify_signup_otp(flow B, step 6),UserService::init_userruns with the real consent/version values already known. It checks for an existing identity row by id (idempotency guard), upserts PII attributes (full_name,email,phone_number) intopii-serviceunder entity typeuser_personal, then inserts the identity row. If the identity-row insert fails after the PII upsert succeeded, a compensating delete removes the just-written PII rather than leaving an orphaned record (SR-027 NFR-06, ADR-00041 Decision 4). - Asynchronous path (idempotent safety net): Firebase’s v1
onCreatebackground trigger (user_account_created.js) fires independently once the account exists. It builds the sameinit_userrequest shape from the account’s own custom claims (terms_accepted,terms_version,privacy_version) andUserRecordfields, and calls the identicalinit_useroperation. Because the synchronous path in step 1 normally already completed by the time this fires,init_user’s existence check makes this call a no-op; if the synchronous path never completed, this call becomes the actual provisioning path instead (ADR-00041, Decision 3). - Both paths report
accept_termsandaccept_privacy_policyfrom the same singleterms_acceptedvalue (SR-027 FR-06; confirmed by design, not a bug — one consent action covers both documents, withterms_version/privacy_versiontracked as independently distinct fields underneath).
End-to-end flow (E) — Gateway App-Check enforcement for pre-auth routes
- A request to any of the four
/auth/otp/*routes carries onlyX-Firebase-AppCheck— noAuthorizationheader exists yet, since the shopper hasn’t signed in. nginx-api-routerrunsauth_request /_app_check_verifyinstead of the normal/_auth_verifyfor these exact-match routes;/_app_check_verifystrips every header exceptX-Firebase-AppCheckand forwards touser-secure-engine’s own/internal/app-check/verify.- A successful verification is cached by the gateway for 5 minutes, keyed by the App Check token value, so repeated OTP calls within a short window don’t re-verify against Firebase’s JWKS endpoint every time.
- Every other route in the system still requires both App Check and a bearer token — this dual-path split exists only for the handful of routes that have no token to present yet.
Why this is simple, and what it does not yet guarantee
This design keeps the OTP mechanism, the consent model, and the account-initialization saga all inside user-secure-engine and its direct dependencies (Firebase Identity Toolkit, pii-service), with no new coordination infrastructure introduced for a two-step saga or a two-path initialization. It does not yet guarantee:
- Per-tenant correctness of the asynchronous trigger path. The
onCreatetrigger sources its tenant identifier from a single hardcoded environment placeholder rather than per-request data — harmless today in a single-tenant deployment, but incorrect the moment a second tenant onboards, see TD-00021. - Full recovery from a failed compensating delete. A failure between the saga’s compensating delete call and its completion is not itself further compensated (ADR-00041, Decision 4) — accepted as a residual, low-probability risk.
- Freshness of adjacent documentation.
user-secure-engine’s own nested service documentation has drifted stale against the current implementation on several points, see TD-00021.
Related Documents
- BRD-026 — Mobile Login & Signup: Phone-Based Identity and Legal Acceptance
- SR-027 — Mobile Login & Signup: System Requirements
- ADR-00041 — Mobile Login & Signup: Phone-OTP, Dual-Path Initialization, and Consent Design
- Technical Debt — Mobile Login & Signup Gaps
- Microservices: user-secure-engine, pii-service, firebase-function