Skip to content

Auth and Protected Routes

  1. User submits credentials on /login.
  2. Dashboard POSTs to POST /api/v1/auth/login.
  3. Backend returns a JWT token and user data.
  4. AuthContext stores the token in localStorage under the key user.
  5. All subsequent API requests include Authorization: Bearer <token>.

AuthContext (src/contexts/AuthContext.tsx) exposes:

PropertyTypeDescription
userobjectDecoded JWT payload including role
tokenstringRaw JWT string
isAdminbooleantrue if user.role === "admin"
isPublisherbooleantrue if user.role === "publisher"
login(data)functionStores user and token, redirects to dashboard
logout()functionClears localStorage, redirects to /login

PrivateRoute (src/router/route.ts) wraps all protected routes and handles three cases:

ScenarioRedirect
No token in localStorage/login
Publisher visiting an admin-only route/publisher/dashboard
Admin visiting a publisher-only route/dashboard