Comprehensive Development Plan: CHS Mobile Frontend & Multi-Tenant API Integration
1. Introduction
This document outlines the strategic plan for developing a modern, mobile-first Next.js application (“CHS Mobile”) that integrates seamlessly with the existing C# backend (“ErpCrystal_CHS.Api”). The focus is on implementing a secure, multi-tenant architecture using Firebase Authentication and JWT tokens, while maintaining the stability of the live production application.
2. Current Architecture Overview
- Backend: ASP.NET Core Web API (.NET 9.0) using Dapper. Multi-tenancy is handled via a
dbnameparameter passed in API routes. - Frontend (Current): Blazor Server application using MudBlazor.
- Database: Multi-tenant SQL Server setup (System, Common, and Client databases).
- Authentication (Current): Azure AD B2C with API Key validation.
3. Targeted Architecture (SOTA)
3.1 Mobile Frontend (Chsmobileversion)
- Framework: Next.js 14/15 (App Router, Server Components).
- UI/UX: Tailwind CSS + shadcn/ui. Mobile-first design with touch-optimized components.
- PWA: Progressive Web App capabilities for “Add to Home Screen” support on iOS and Android.
- State Management: TanStack Query (fetching/caching) and Zustand (client-side state).
- Authentication: Firebase Client SDK integration.
3.2 Backend Integration (ErpCrystal_CHS.Api)
- JWT Verification Service: A centralized middleware/service to process Firebase JWTs.
- Tenant Context: Automatic extraction of
dbnamefrom JWT custom claims. - Security Logic: Mandatory validation ensuring the requested
dbnamematches the authenticated user’s assigned database.
4. Implementation Strategy
Phase 1: Security & Multi-Tenancy (C# Backend)
Goal: Enable JWT-based multi-tenancy without disrupting the Blazor application.
- Firebase Integration:
- Configure
Microsoft.AspNetCore.Authentication.JwtBearerto validate Firebase tokens. - Setup
AuthorityandTokenValidationParametersfor the Firebase Project.
- Configure
- Tenant Middleware (
FirebaseJwtMiddleware.cs):- Intercept all incoming requests.
- Extract
dbnamefrom theclaimsin the JWT. - Store it in
HttpContext.Items["ResolvedDbName"].
- DapperContext Enhancment:
- Modify
DapperContextto useIHttpContextAccessor. - Update
SetClientConnection(string dbname)to cross-verify the passeddbnamewith theResolvedDbNamefrom the JWT. - If no
dbnameis passed (future-proofing), use theResolvedDbNameautomatically.
- Modify
Phase 2: Next.js Mobile Foundation
Goal: Setup the core application shell and auth flow.
- Project Initialization:
npx create-next-appwith TypeScript, Tailwind, and App Router.- Install
shadcn/uicomponents (Sheet, Nav, BottomTab, etc.).
- Auth Integration:
- Implement
FirebaseProvideranduseAuthhook. - Create a clean Login page using
shadcn/uiForm and Card.
- Implement
- API Client Layer:
- Build a Fetch wrapper (using Interceptors) that automatically appends the
dbnameto the URL and injects theBearertoken. - Handle 401/403 errors by redirecting to login.
- Build a Fetch wrapper (using Interceptors) that automatically appends the
Phase 3: PWA & Mobile UX
Goal: Ensure the app feels like a native mobile application.
- Installability:
- Configure
manifest.jsonwith appropriate icons anddisplay: standalone. - Implement Service Worker for asset caching (offline support).
- Configure
- Mobile UX Polish:
- Implement “Bottom Navigation” for primary app actions.
- Use “Pull-to-Refresh” and “Swipe Actions” where applicable.
- Optimize images and fonts for fast mobile loading.
5. Technical Decision Matrix
| Feature | Recommended | Rationale |
|---|---|---|
| Auth Provider | Firebase Auth | Scalable, handles custom claims for dbname, and hosts the frontend. |
| Data Fetching | TanStack Query | Superior caching and synchronization for mobile networks. |
| Database Routing | JWT Claims | Authoritative and secure; prevents users from accessing other DBs via URL spoofing. |
| PWA Config | Native Manifest | Better control over iOS/Android specific meta tags. |
6. Risk Mitigation & Compliance
- Stable Production: All C# changes will be “add-only” or “fallback-safe” to ensure the existing Blazor app never breaks.
- Cross-Tenant Security: The backend must enforce that the JWT
dbnameclaim matches the request. If a user tries to access a differentdbname, the API will return 401 even if the API Key is valid. - SSL/TLS: All communications will be over HTTPS, especially since tokens are passed in headers.
7. Immediate Next Steps
- Verify Firebase Project settings and custom claims structure.
- Draft the
FirebaseJwtMiddlewarein a separate branch ofErpCrystal_CHS.Api. - Initialize the Next.js repository in the
Chsmobileversionfolder. - Conduct a POC call from Next.js to the C# API with a valid token.