How rajukarki/react-django-portal works
What this project is (type + purpose)
This is a full-stack “portal” starter:
• Backend: Django + Django REST Framework (DRF) exposing an auth-centric REST API.
• Auth mechanism: JWT via SimpleJWT with refresh tokens and blacklisting for logout.
• Frontend: React (Vite) SPA using React Router and Axios interceptors.
• Primary use case: register/login, store tokens client-side, protect routes on both FE + BE, and demonstrate a protected Dashboard.
• Browser SPA ↔ REST API over HTTP
• JWT access/refresh lifecycle (attach Bearer token; refresh on expiry; blacklist on logout)
• CORS/dev-server separation
• Persistence (SQLite + Django auth tables + token blacklist tables)
Read the structure as architecture boundaries
From the tree:
Backend boundary (backend/)
• backend/core/ is the Django project: settings, URL routing, ASGI/WSGI entrypoints.
• backend/accounts/ is the domain app for authentication APIs:
• models.py (likely leveraging Django User or extensions)
• serializers.py (DRF serialization / validation)
• views.py (register/login/logout/dashboard endpoints)
• urls.py (mounts app routes under /api/v1/user/)
• utils.py (support logic—often token helpers, etc.)
• requirements.txt indicates key dependencies: DRF, SimpleJWT, blacklist app, cors headers, decouple, etc.
• DB: README implies db.sqlite3 exists (dev persistence).
Frontend boundary (frontend/)
• Vite app with:
• src/App.jsx (route table)
• AuthProvider.jsx (auth state derived from tokens in localStorage)
• AxiosInstance.js (base URL from VITE_BACKEND_BASE_API, injects Bearer token, refresh flow)
• PrivateRoute.jsx / PublicRoute.jsx (route guards)
• UI components: Login, Register, Dashboard, layout bits.
React SPA (Vite build)
• Router layer (React Router)
• Auth state layer (AuthProvider)
• Route guards (PrivateRoute, PublicRoute)
• HTTP client layer (AxiosInstance w/ interceptors)
Storage
• localStorage: accessToken, refreshToken (per README behavior)
Django entrypoints
• WSGI/ASGI (deployment boundary)
Django URL routing
• core/urls.py mounts /api/v1/user/…
• accounts/urls.py defines endpoints
Accounts app (DRF layer)
• Views (controllers)
• Serializers (validation + shaping payloads)
• Permissions (IsAuthenticated for dashboard)
Auth/token subsystem
• SimpleJWT: issue access/refresh
• Refresh endpoint: mints new access token
• Blacklist app: stores revoked refresh tokens (logout)
Database
• SQLite (dev)
• Data sets: auth_user, token blacklist tables, etc.
Environment configuration
• Backend .env via python-decouple (SECRET_KEY, DEBUG)
• Frontend .env via Vite (VITE_BACKEND_BASE_API)
Registration flow
• SPA Register.jsx → AxiosInstance → POST /api/v1/user/register/
• Backend accounts.views → accounts.serializers → create User → persist in DB
• Response back to SPA (success/failure)
Login flow (token issuance)
• SPA Login.jsx → POST /api/v1/user/login/
• Backend authenticates credentials
• Backend returns { access, refresh }
• SPA stores tokens in localStorage
• SPA updates auth state (AuthProvider)
Authenticated request flow (Bearer token)
• SPA → GET /api/v1/user/dashboard/
• Axios interceptor attaches Authorization: Bearer <access>
• Backend DRF permission IsAuthenticated validates JWT
• Backend returns dashboard payload
Access token expiry → refresh-token flow
• SPA calls protected endpoint → receives 401 (expired access)
• Axios interceptor triggers refresh:
• POST /api/v1/user/token/refresh/ (or your custom path, but README lists SimpleJWT route)
• Backend validates refresh token → returns new access
• SPA updates localStorage and retries original request
Logout flow (blacklist)
• SPA → POST /api/v1/user/logout/ with refresh token
• Backend blacklists refresh token in DB
• SPA clears tokens from localStorage
Architectural patterns worth highlighting (specific to this repo)
• SPA + REST API separation (two deployables, two dev servers).
• Token-based stateless auth (access token) + stateful revocation (refresh token blacklist).
• Layered backend: URLs → Views → Serializers → DB.
• Client-side cross-cutting concern: Axios interceptors centralize auth headers + refresh retries.
• Dual protection:
• Frontend: PrivateRoute/PublicRoute
• Backend: DRF permissions for protected endpoints
• Browser (React SPA)
• Django REST API
• SQLite DB
• (Optional) “Token Blacklist tables” as a labeled part of DB
• SPA → API (HTTP JSON)
• API ↔ DB (ORM)
Annotations:
• JWT Bearer auth
• CORS allowed origin (dev)
Participants:
• User
• React UI (Login/Register/Dashboard)
• AxiosInstance (interceptor)
• Django API (accounts + SimpleJWT)
• DB
Sequences for login, dashboard call, refresh, logout.
Frontend labels
• “Routing”: frontend/src/App.jsx
• “Auth state”: frontend/src/AuthProvider.jsx
• “Route guards”: frontend/src/PrivateRoute.jsx, frontend/src/PublicRoute.jsx
• “HTTP client + refresh”: frontend/src/AxiosInstance.js
• “Screens”: frontend/src/components/Login.jsx, Register.jsx, dashboard/Dashboard.jsx
Backend labels
• “Project config”: backend/core/settings.py
• “API routing”: backend/core/urls.py, backend/accounts/urls.py
• “Auth API”: backend/accounts/views.py, serializers.py
• “Data model”: backend/accounts/models.py (and Django User)
• “Token system”: SimpleJWT + blacklist (dependency; configured in settings)
Elements explicitly:
• Blue rectangles: frontend components
• Green rectangles: backend components
• Red note/annotation: “401 triggers refresh flow”
• Trust boundaries:
• Browser boundary (untrusted client)
• Server boundary (trusted)
• Storage boundary (localStorage is client-controlled)
“Don’t overthink it” decomposition checklist (capture as many components as possible)
• React Router
• AuthProvider (isLoggedIn)
• localStorage tokens
• Axios interceptors (attach token + refresh)
• Django URL routing (core → accounts)
• DRF views/serializers
• DRF permission on dashboard
• SimpleJWT token issue + refresh
• Token blacklist on logout
• SQLite persistence
• Env vars on both sides
• CORS