stronk_8s

MONIVO USER FLOW

Dec 17th, 2025
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. Monivo Frontend Flow (Authentication and Data Pipeline)
  2.  
  3. Tech stack (frontend)
  4. - Expo Router (app directory), React Native, React Context
  5. - AsyncStorage for tokens/user data
  6. - Fetch-based API wrapper (services/apiService.ts)
  7. - Environment-driven config (config/environment.ts), persisted user id (config/userconfig.ts)
  8.  
  9. 1) Authentication flow (from app start to end-user UI)
  10. - Root composition (app/_layout.tsx):
  11. - Wraps app with ThemeProvider, AuthProvider, UserProvider, then AuthGuard around the navigation Stack/Tabs.
  12.  
  13. - AuthGuard (components/auth/AuthGuard.tsx):
  14. - While auth state is loading → shows LoadingScreen.
  15. - If not authenticated → shows WelcomeScreen.
  16. - If authenticated → renders the protected tabs UI.
  17.  
  18. - Welcome/login (components/auth/WelcomeScreen.tsx):
  19. - Presents a Google-like flow where the user selects an account (mocked list).
  20. - On account selection: calls setDefaultUserId(<chosenId>) → stores in AsyncStorage.
  21. - Then calls login().
  22.  
  23. - AuthProvider (components/auth/AuthProvider.tsx):
  24. - checkAuthStatus():
  25. - Reads token via authService.getAuthToken(). If missing → unauthenticated.
  26. - If token exists → authService.initialize() sets the Authorization header on apiService.
  27. - Loads user from storage; if found → authenticated; else tries refreshToken() and finally clears storage if still invalid.
  28. - login():
  29. - Uses Config.DEFAULT_USER_ID (or the id set by WelcomeScreen via setDefaultUserId) to fetch real user data from backend with userService.getUserById.
  30. - Stores a dev access token + refresh token + user in AsyncStorage under @monivo/* keys.
  31. - Sets isAuthenticated = true and exposes user in context.
  32. - logout(): clears all auth data via authService.clearAuthData() and resets context state.
  33.  
  34. - Storage keys (services/storageService.ts):
  35. - @monivo/auth_token, @monivo/refresh_token, @monivo/user_data
  36.  
  37. - User id resolution (config/userconfig.ts):
  38. - getUserId(): returns the stored DEFAULT_USER_ID from AsyncStorage if available; otherwise falls back to Config.DEFAULT_USER_ID.
  39. - setDefaultUserId(id): persists the chosen id for subsequent API calls.
  40.  
  41. 2) Data fetching pipeline (from API to UI)
  42. - Environment/config (config/environment.ts):
  43. - Supplies API_BASE_URL, API_VERSION, DEFAULT_USER_ID.
  44. - getFullApiUrl() used by apiService to build URLs; isDevelopment() logs configured values.
  45.  
  46. - API service (services/apiService.ts):
  47. - Central fetch wrapper with default JSON headers and optional Authorization.
  48. - Provides generic get/post/put/patch/delete and uploadFile.
  49. - Exposes grouped endpoint helpers (authAPI, userAPI, budgetAPI, transactionAPI, categoryAPI) using relative endpoints.
  50.  
  51. - Domain services (services/*):
  52. - transactionService:
  53. - getTransactionsByUserId(userId): fetches from https://monivo-node-api.onrender.com/api/transactions/user/:userId
  54. - Maps backend records to app Transaction model, including populated category.
  55. - CRUD routes via apiService for non user-scoped paths when used.
  56. - budgetService:
  57. - getBudgetsByUserId(userId): fetches from https://monivo-node-api.onrender.com/api/budgets/user/:userId
  58. - Maps budgets and optionally populated category; also exposes CRUD via apiService.
  59. - categoryService:
  60. - getCategoriesByUserId(userId): fetches from https://monivo-node-api.onrender.com/api/categories/user/:userId and maps to Category model; CRUD via apiService.
  61. - dashboardService:
  62. - Aggregates data by calling the above 3 services in parallel.
  63. - Computes financialSummary (income, expenses, budget, savings), recentTransactions, categoryBreakdown, and monthlyTrends.
  64.  
  65. - Hooks (client-side shape + state):
  66. - use-transaction-data.tsx:
  67. - Resolves userId via getUserId(), fetches transactions by user, sorts by date, computes totals, exposes { transactions, summary, loading, error, refetch }.
  68. - use-budget-data.tsx:
  69. - Resolves userId via getUserId(), fetches budgets + transactions by user.
  70. - Builds per-category spend (by month-year) and monthly summary; exposes { budgets, summary, loading, error, refetch }.
  71.  
  72. 3) Data display (screens and components)
  73. - Tabs layout (app/(tabs)/_layout.tsx): sets themed headers and defines the 4 tabs (overview, transaction, budget, profile).
  74.  
  75. - Overview screen (app/(tabs)/overview/index.tsx):
  76. - Loads userId via getUserId() and calls dashboardService.getDashboardData(userId).
  77. - Sets state for financialSummary, recentTransactions, spendingTrends.
  78. - Renders OverviewCard totals, SpendingTrendsChart, and RecentTransactionsCard.
  79.  
  80. - Transactions screen (app/(tabs)/transaction/index.tsx):
  81. - Uses use-transaction-data to load/transform transactions and summary.
  82. - Supports search and type filtering; shows list and TransactionDetailsModal.
  83. - Uses useFocusEffect to refetch on screen focus.
  84.  
  85. - Budgets UI (example: app/modals/EditBudgetModal.tsx):
  86. - Builds UpdateBudgetRequest with amount, month, year, and userId from getUserId() or Config.DEFAULT_USER_ID.
  87. - Calls budgetService.updateBudget(budget.id, payload), then triggers onBudgetEdited() and onClose().
  88. - Note: current code calls onClose both in try and in finally when onClose exists; ensure your parent handler tolerates being called twice or adjust modal logic.
  89.  
  90. 4) End-to-end flow summary
  91. 1. App starts → AuthGuard checks tokens:
  92. - If no valid token/user → WelcomeScreen is shown.
  93. - User selects an account → setDefaultUserId(id) → login() fetches user and stores tokens → authenticated.
  94. 2. Tabs render; each screen resolves userId via getUserId() (or env fallback).
  95. 3. Services fetch user-scoped resources (transactions, budgets, categories) and map to UI models.
  96. 4. Hooks/screens transform data (sorting, aggregations), maintain loading/error state.
  97. 5. Components render cards/lists/charts. CRUD actions call services and then refetch or update local state.
  98.  
Advertisement
Add Comment
Please, Sign In to add comment