SAMT is a lightweight farmer-focused web application that provides market price tracking, weather insights, a community forum, and administrative tools to manage market items.
- Purpose: Help farmers discover market prices, get weather-aware advice, share knowledge, and allow admins to manage market listings.
- Audience: Small-to-medium farmers, agricultural extension workers, and marketplace administrators.
- Primary workflows: Browse market items, view weather & actionable advice, register/login as farmer or admin, create forum posts and comments, admin CRUD on market items.
- Frontend: React + TypeScript (Vite), shadcn/ui components, Tailwind CSS, React Router v6, TanStack Query, Sonner toasts.
- Backend: Node.js + Express, MongoDB (Mongoose), JWT auth, cookie support.
- HTTP client: Axios (shared instance in ui/src/api/axios.js).
- Dev tooling: Vite, ESLint, TypeScript.
Backend (server):
- Open a terminal in
backend/. - Create a
.envwith at least:PORT=3000MONGO_URI=<your mongo uri>JWT_SECRET=<your secret>CORS=http://localhost:5173(or your frontend origin)
- Install & run:
cd backend
npm install
npm run dev # or `node server.js` for production runFrontend (UI):
- Open a terminal in
ui/. - Install & run:
cd ui
npm install
npm run dev- The frontend expects the backend API base at
http://localhost:3000/apiby default. Override withVITE_API_BASE_URLin.envif needed.
- Market Dashboard: View, search and filter market items by region and name.
- Weather Insights: City-level weather and simple advice generation using backend rules.
- Forum: Authenticated users can create posts and comments; posts are populated with author info.
- Authentication: JWT-based login/register (cookies set by server for convenience), role-aware routes (farmer/admin).
- Admin Panel: Add, edit, delete market items with image uploads.
- Toasts & Error Handling: Centralized API error extraction and session-expiry handling in the frontend.
- Keep the UI logic thin: use a single Axios instance (
ui/src/api/axios.js) with response interceptors for auth expirations. - Server issues are surfaced to the user via consistent error messages (see
ui/src/lib/apiError.ts). - Minimal state in the AuthContext (token, role, name) persisted to
localStoragefor simple sessions. - Protect routes with a
ProtectedRoutewrapper that checksuser.roleandloadingstate.
- Frontend must point to the running backend port (default now
http://localhost:3000/api). VerifyVITE_API_BASE_URLwhen deploying. - Registration: frontend now sends
username,email,role, and required fields to match backendUserschema. If backend requires additional fields, update the form and payload. - Auth error handling: on 401/403 the frontend clears local auth and dispatches an
auth:expiredevent — apps should listen for it if using multiple windows. - Forum posts: ensure authenticated requests include either Authorization header (
Bearer <token>) or cookies; the server accepts both. The frontend now sends posts without using the JWT as the author — server usesreq.userfrom the middleware. - UI improvements: clear form errors when switching tabs or reopening modals (implemented).
- If login/register returns
User already existsorValidation failed, inspect backend logs and the payload sent by the UI (browser Network tab). - If CORS errors appear, confirm
CORSin backend.envincludes the exact frontend origin (scheme+host+port). - If images/uploads fail, ensure backend cloudinary or upload config is present and valid in
backend/config/cloudinary.js.
- Add E2E tests for auth and forum flows to catch regressions early.
- Harden token refresh and graceful logout flows (refresh tokens or silent re-auth patterns).
- Audit all API calls to standardize error messages and codes from the backend.
Created/updated files:
ui/src/lib/apiError.ts— common API error extractorui/src/api/axios.js— axios instance and auth-expiry interceptorui/src/context/AuthContext.tsx— auth state + expired handlerui/src/components/LoginModal.tsx— login/register modal fixes
If you want, I can:
- Run a local build and smoke-test the full auth flow.
- Add example
.envfiles for frontend and backend. - Open a checklist PR with the fixes grouped and tests added.
If you'd like adjustments to tone or additional sections (architecture diagram, sequence diagrams, API contract), tell me which and I will add them.