Skip to content

feat: Include token in session response and store in localStorage for…#4

Merged
KD2303 merged 1 commit into
mainfrom
master
Mar 20, 2026
Merged

feat: Include token in session response and store in localStorage for…#4
KD2303 merged 1 commit into
mainfrom
master

Conversation

@KD2303
Copy link
Copy Markdown
Owner

@KD2303 KD2303 commented Mar 20, 2026

… authentication

Copilot AI review requested due to automatic review settings March 20, 2026 08:54
@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
skillflare Ready Ready Preview, Comment Mar 20, 2026 8:54am

@KD2303 KD2303 merged commit ee2182b into main Mar 20, 2026
10 of 14 checks passed
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds JWT token propagation to the frontend for Socket.IO authentication by including the token in backend auth/session responses and persisting it client-side.

Changes:

  • Backend: include token in sendTokenResponse JSON payload.
  • Backend: include token in /api/auth/session response when authenticated.
  • Frontend: store/clear token in localStorage during session check, login/register, and logout.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
frontend/src/context/AuthContext.jsx Persists response.token to localStorage on auth events and clears it on logout/errors.
backend/src/utils/tokenUtils.js Adds the JWT token field to the JSON body returned alongside the auth cookie.
backend/src/controllers/authController.js Adds token to the authenticated /auth/session response.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

.json({ success: true, authenticated: false, user: null });

res.status(200).json({ success: true, authenticated: true, user });
res.status(200).json({ success: true, authenticated: true, user, token });
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getSession is echoing the cookie JWT back in the response body, which exposes the primary auth token to client-side JavaScript and undermines the httpOnly cookie model. If Socket.IO needs auth, prefer cookie-based socket auth (parse cookie server-side) or issue a short-lived secondary token rather than returning the session JWT here.

Suggested change
res.status(200).json({ success: true, authenticated: true, user, token });
// Issue a short-lived secondary token instead of exposing the primary cookie JWT
const secondaryToken = jwt.sign(
{ id: user.id, purpose: "session" },
process.env.JWT_SECRET,
{ expiresIn: "5m" },
);
res
.status(200)
.json({ success: true, authenticated: true, user, token: secondaryToken });

Copilot uses AI. Check for mistakes.
Comment on lines +37 to +42
// Store token in localStorage if authenticated for Socket.io connection
if (response.authenticated && response.token) {
localStorage.setItem("token", response.token);
} else {
localStorage.removeItem("token");
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Storing the auth JWT in localStorage makes it retrievable by any XSS payload and increases the blast radius compared to an httpOnly cookie. If the goal is Socket.IO auth, consider authenticating the socket using the existing cookie (send credentials in the handshake and validate cookies server-side) or keep the token only in memory for the current tab/session instead of persistent storage.

Copilot uses AI. Check for mistakes.
Comment on lines +57 to +60
// Store token in localStorage for Socket.io and other client-side usage
if (response.token) {
localStorage.setItem("token", response.token);
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On successful login, the token is only written when response.token is truthy; if the backend ever omits token (or a proxy strips it), an older token already in localStorage would remain and could be used for Socket.IO auth. Consider explicitly removing/overwriting the stored token when the login succeeds but no token is present.

Copilot uses AI. Check for mistakes.
setIsAuthenticated(true);
// Store token in localStorage for Socket.io and other client-side usage
if (response.token) {
localStorage.setItem("token", response.token);
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On successful registration, the token is only written when response.token is truthy; if no token is returned, any previous localStorage token would remain and could be used by Socket.IO. Consider explicitly clearing/overwriting the stored token when registration succeeds but no token is present.

Suggested change
localStorage.setItem("token", response.token);
localStorage.setItem("token", response.token);
} else {
localStorage.removeItem("token");

Copilot uses AI. Check for mistakes.
.json({ success: true, authenticated: false, user: null });

res.status(200).json({ success: true, authenticated: true, user });
res.status(200).json({ success: true, authenticated: true, user, token });
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change adds a new token field to the /auth/session response but there are no integration tests covering that endpoint. Add/update tests to validate the session response shape (authenticated true/false) and ensure the token behavior matches the intended security model.

Suggested change
res.status(200).json({ success: true, authenticated: true, user, token });
res.status(200).json({ success: true, authenticated: true, user });

Copilot uses AI. Check for mistakes.
Comment on lines +46 to 48
token, // Include token for Socket.io and client-side storage
});
};
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning the raw JWT in the JSON response negates the protection provided by an httpOnly cookie and makes the token available to any injected script (XSS), enabling easy token exfiltration/replay. Prefer keeping the JWT only in an httpOnly cookie and authenticating Socket.IO via cookies on the handshake, or mint a separate short-lived, socket-scoped token instead of exposing the primary auth JWT.

Suggested change
token, // Include token for Socket.io and client-side storage
});
};
});
};
};

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants