fix: gate /recommendations API calls behind authenticated session#796
fix: gate /recommendations API calls behind authenticated session#796saniya196 wants to merge 1 commit into
Conversation
📝 WalkthroughWalkthroughThree student module pages now gate ChangesAuthentication-gated recommendation endpoints
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
client/src/module/student/learn/LearnHubPage.tsx (1)
28-40: ⚡ Quick winConsider extracting a shared recommendations hook to avoid re-duplicating the gate.
This same auth/role-gated
/student/recommendationsfetch now appears in three pages (LearnHubPage,RoadmapCanvasPage,RoadmapDashboardPage). A small shared hook (e.g.useWeakAreas()returning the gated rawweakAreas) would centralize the gate so a future fourth caller can't re-introduce the 401 bug; each page can still apply its own transform (Set of slugs in Canvas, slicing in the others).🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@client/src/module/student/learn/LearnHubPage.tsx` around lines 28 - 40, Extract the duplicated auth/role-gated fetch into a shared hook named useWeakAreas that encapsulates the gate and network call: move the logic from the useEffect (the isAuthenticated/userRole check, setLoadingRecs state, api.get('/student/recommendations') call, and error/finally behavior) into useWeakAreas and have it return the raw weakAreas array and loading flag (e.g., { weakAreas, loadingRecs }) so LearnHubPage (and RoadmapCanvasPage/RoadmapDashboardPage) can call useWeakAreas() and apply their own transforms; ensure the hook uses the same API call and preserves the behavior of setting [] on unauthenticated or on error and toggling the loading state.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@client/src/module/student/learn/LearnHubPage.tsx`:
- Around line 28-40: Extract the duplicated auth/role-gated fetch into a shared
hook named useWeakAreas that encapsulates the gate and network call: move the
logic from the useEffect (the isAuthenticated/userRole check, setLoadingRecs
state, api.get('/student/recommendations') call, and error/finally behavior)
into useWeakAreas and have it return the raw weakAreas array and loading flag
(e.g., { weakAreas, loadingRecs }) so LearnHubPage (and
RoadmapCanvasPage/RoadmapDashboardPage) can call useWeakAreas() and apply their
own transforms; ensure the hook uses the same API call and preserves the
behavior of setting [] on unauthenticated or on error and toggling the loading
state.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: aa1d89d4-3bf5-467a-a803-16e62a736fe3
📒 Files selected for processing (3)
client/src/module/student/learn/LearnHubPage.tsxclient/src/module/student/roadmap/RoadmapCanvasPage.tsxclient/src/module/student/roadmap/RoadmapDashboardPage.tsx
Sachinchaurasiya360
left a comment
There was a problem hiding this comment.
Review: Approved
Correct fix for the 401 console error on the Sign In page. Gating the /recommendations API call behind isAuthenticated (from useAuthStore) ensures the request only fires when a valid session exists.
What's good:
- Applies the same guard to all three call sites (
LearnHubPage,RoadmapCanvasPage,RoadmapDashboardPage) - Explicitly clears the loading state for unauthenticated users so the UI doesn't hang
- No changes to the actual recommendations logic — pure guard placement
Note for merge: RoadmapCanvasPage.tsx is also modified by PR #793 (skipped status). After the other PR resolves, a rebase may be needed.
Fixes #769
🚀 Description
The /recommendations endpoint was firing on the Sign In page
before any auth token was available, causing a 401 Unauthorized
error in the console.
Patched three call sites:
Changes:
the UI doesn't wait on a skipped request
🔨 Type of change
✅ Checklist
Summary by CodeRabbit