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

Mobile Login & Signup — component topology and data flow across ui-application's pre_auth feature, the CDN/Nginx gateway, user-secure-engine's phone-OTP login/signup, the dual-path account-initialization saga, and pii-service

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

  1. The app opens to a splash screen while ui-application initializes (Firebase SDK, App Check activation, session check).
  2. 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.
  1. 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).
  2. On submit, phone_auth_repository.dart calls POST /auth/otp/signup/send. The gateway verifies App Check only (/_app_check_verify, no bearer token) before forwarding to user-secure-engine (SR-027 FR-09).
  3. AuthService::send_signup_otp looks 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 under OtpPurpose::PhoneSignup, and sent, subject to cooldown/hourly rate limiting (SR-027 NFR-01/NFR-02).
  4. The shopper enters the code on the OTP-verify screen (otp_verify_controller.dart, signup mode); phone_auth_repository.dart calls POST /auth/otp/signup/verify with the code and the consent flag/version identifiers captured in step 1.
  5. AuthService::verify_signup_otp rejects immediately if the consent flag is false (SR-027 FR-04). On a successful code match, it calls create_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.
  6. Still inside the same request, verify_signup_otp sets custom claims (role, terms_accepted, terms_version, privacy_version) on the new Firebase account, then synchronously calls UserService::init_user with those same values — this is the authoritative path for the users row and PII attributes (flow D), run before the trigger in flow C can possibly fire with correct data.
  7. verify_signup_otp mints a Firebase custom token and returns it to the client, which completes sign-in via signInWithCustomToken → saveUserSession (shared _completeSignIn).

End-to-end flow (C) — Login: existence check, OTP send/verify, no writes

  1. The shopper enters a phone number on the login screen (login_single_controller.dart); phone_auth_repository.dart calls POST /auth/otp/login/send, gated by the same App-Check-only enforcement as signup.
  2. AuthService::send_login_otp looks 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 under OtpPurpose::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).
  3. The shopper enters the code (otp_verify_controller.dart, login mode); phone_auth_repository.dart calls POST /auth/otp/login/verify.
  4. AuthService::verify_login_otp verifies 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).
  5. The client completes sign-in via the same shared _completeSignIn as signup.

End-to-end flow (D) — Dual-path account initialization saga

  1. Synchronous path (source of truth): Inside verify_signup_otp (flow B, step 6), UserService::init_user runs 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) into pii-service under entity type user_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).
  2. Asynchronous path (idempotent safety net): Firebase’s v1 onCreate background trigger (user_account_created.js) fires independently once the account exists. It builds the same init_user request shape from the account’s own custom claims (terms_accepted, terms_version, privacy_version) and UserRecord fields, and calls the identical init_user operation. 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).
  3. Both paths report accept_terms and accept_privacy_policy from the same single terms_accepted value (SR-027 FR-06; confirmed by design, not a bug — one consent action covers both documents, with terms_version/privacy_version tracked as independently distinct fields underneath).

End-to-end flow (E) — Gateway App-Check enforcement for pre-auth routes

  1. A request to any of the four /auth/otp/* routes carries only X-Firebase-AppCheck — no Authorization header exists yet, since the shopper hasn’t signed in.
  2. nginx-api-router runs auth_request /_app_check_verify instead of the normal /_auth_verify for these exact-match routes; /_app_check_verify strips every header except X-Firebase-AppCheck and forwards to user-secure-engine’s own /internal/app-check/verify.
  3. 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.
  4. 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 onCreate trigger 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.