fix(session): redirect to sign-in when session expires silently#1403
Conversation
|
@mallya-m is attempting to deploy a commit to the PRIYANSHU DOSHI's projects Team on Vercel. A member of the Team first needs to authorize it. |
GSSoC Label Checklist 🏷️@Priyanshu-byte-coder — please apply the appropriate labels before merging: Difficulty (pick one):
Quality (optional):
Validation (required to score):
|
|
@Priyanshu-byte-coder could you please add |
Priyanshu-byte-coder
left a comment
There was a problem hiding this comment.
Thanks for working on session expiry handling! The concept is right but the implementation has issues:
window.fetch monkey-patching is risky:
window.fetch = async (...args) => {
const response = await originalFetch(...args);
if (response.status === 401) { // intercepts ALL fetch callsThis intercepts every fetch in the dashboard including third-party API calls (GitHub, etc.). A 401 from GitHub's API would incorrectly sign the user out of DevTrack.
Better approach:
- Use NextAuth's middleware (already in
src/middleware.ts) to redirect unauthenticated users - For specific session-expiry UX, use
useSession({ required: true, onUnauthenticated() { router.push('/auth/signin') } }without the fetch intercept
Please revise to avoid monkey-patching the global fetch.
Priyanshu-byte-coder
left a comment
There was a problem hiding this comment.
The session-expiry redirect logic is useful but the implementation monkey-patches window.fetch globally in a React component. This is a risky pattern that can cause flaky behavior and interfere with other fetches on the page.
Preferred approach: Use a custom fetch wrapper utility or an Axios interceptor in a dedicated module (e.g. src/lib/auth-fetch.ts), then import it where needed. Or use next-auth's useSession({ required: false }) combined with an error === 'TokenRevoked' check (already supported by the JWT callback) to trigger redirect without patching globals.
Please refactor to avoid patching window.fetch and rebase on main.
99863e2
into
Priyanshu-byte-coder:main
|
🎉 Merged! Thanks for contributing to DevTrack. If the project has been useful to you, a ⭐ star on the repo is the easiest way to support it — it helps DevTrack get discovered by more developers. Keep an eye on open issues for your next contribution! |
What does this PR do?
Fixes silent 401 failures on the dashboard. When any API call returns 401 (session expired), the user now sees a toast "Session expired. Please sign in again." and is redirected to the sign-in page.
Related issue
Closes #930
Changes made
src/app/dashboard/layout.tsxscoped to /dashboard routesuseSession({ required: true })as primary session guardHow to test