-
Notifications
You must be signed in to change notification settings - Fork 2
feat: Include token in session response and store in localStorage for… #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -127,7 +127,7 @@ export const getSession = asyncHandler(async (req, res) => { | |||||
| .status(200) | ||||||
| .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 }); | ||||||
|
||||||
| res.status(200).json({ success: true, authenticated: true, user, token }); | |
| res.status(200).json({ success: true, authenticated: true, user }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -43,5 +43,6 @@ export const sendTokenResponse = (user, statusCode, res) => { | |||||||||||||
| res.status(statusCode).cookie("token", token, options).json({ | ||||||||||||||
| success: true, | ||||||||||||||
| user: userData, | ||||||||||||||
| token, // Include token for Socket.io and client-side storage | ||||||||||||||
| }); | ||||||||||||||
| }; | ||||||||||||||
|
Comment on lines
+46
to
48
|
||||||||||||||
| token, // Include token for Socket.io and client-side storage | |
| }); | |
| }; | |
| }); | |
| }; | |
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -34,9 +34,16 @@ export const AuthProvider = ({ children }) => { | |||||||||
| const response = await authService.getSession(); | ||||||||||
| setUser(response.user || null); | ||||||||||
| setIsAuthenticated(Boolean(response.authenticated)); | ||||||||||
| // Store token in localStorage if authenticated for Socket.io connection | ||||||||||
| if (response.authenticated && response.token) { | ||||||||||
| localStorage.setItem("token", response.token); | ||||||||||
| } else { | ||||||||||
| localStorage.removeItem("token"); | ||||||||||
| } | ||||||||||
|
Comment on lines
+37
to
+42
|
||||||||||
| } catch { | ||||||||||
| setUser(null); | ||||||||||
| setIsAuthenticated(false); | ||||||||||
| localStorage.removeItem("token"); | ||||||||||
| } finally { | ||||||||||
| setLoading(false); | ||||||||||
| } | ||||||||||
|
|
@@ -47,6 +54,10 @@ export const AuthProvider = ({ children }) => { | |||||||||
| const response = await authService.login(email, password); | ||||||||||
| setUser(response.user); | ||||||||||
| setIsAuthenticated(true); | ||||||||||
| // Store token in localStorage for Socket.io and other client-side usage | ||||||||||
| if (response.token) { | ||||||||||
| localStorage.setItem("token", response.token); | ||||||||||
| } | ||||||||||
|
Comment on lines
+57
to
+60
|
||||||||||
| toast.success(`Welcome back, ${response.user.name}!`); | ||||||||||
| return response; | ||||||||||
| } catch (error) { | ||||||||||
|
|
@@ -65,6 +76,10 @@ export const AuthProvider = ({ children }) => { | |||||||||
| const response = await authService.register(userData); | ||||||||||
| setUser(response.user); | ||||||||||
| setIsAuthenticated(true); | ||||||||||
| // Store token in localStorage for Socket.io and other client-side usage | ||||||||||
| if (response.token) { | ||||||||||
| localStorage.setItem("token", response.token); | ||||||||||
|
||||||||||
| localStorage.setItem("token", response.token); | |
| localStorage.setItem("token", response.token); | |
| } else { | |
| localStorage.removeItem("token"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getSessionis 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.