Auth and Protected Routes
Login Flow
Section titled “Login Flow”- User submits credentials on
/login. - Dashboard POSTs to
POST /api/v1/auth/login. - Backend returns a JWT token and user data.
AuthContextstores the token inlocalStorageunder the keyuser.- All subsequent API requests include
Authorization: Bearer <token>.
AuthContext
Section titled “AuthContext”AuthContext (src/contexts/AuthContext.tsx) exposes:
| Property | Type | Description |
|---|---|---|
user | object | Decoded JWT payload including role |
token | string | Raw JWT string |
isAdmin | boolean | true if user.role === "admin" |
isPublisher | boolean | true if user.role === "publisher" |
login(data) | function | Stores user and token, redirects to dashboard |
logout() | function | Clears localStorage, redirects to /login |
PrivateRoute Behavior
Section titled “PrivateRoute Behavior”PrivateRoute (src/router/route.ts) wraps all protected routes and handles three cases:
| Scenario | Redirect |
|---|---|
| No token in localStorage | /login |
| Publisher visiting an admin-only route | /publisher/dashboard |
| Admin visiting a publisher-only route | /dashboard |