From ee2182b33865edbdcc52671b60b8da84719d9747 Mon Sep 17 00:00:00 2001 From: KD2303 Date: Fri, 20 Mar 2026 14:23:54 +0530 Subject: [PATCH] feat: Include token in session response and store in localStorage for authentication --- backend/src/controllers/authController.js | 2 +- backend/src/utils/tokenUtils.js | 1 + frontend/src/context/AuthContext.jsx | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/backend/src/controllers/authController.js b/backend/src/controllers/authController.js index 1bcdcf8..fc44369 100644 --- a/backend/src/controllers/authController.js +++ b/backend/src/controllers/authController.js @@ -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 }); } catch { res.status(200).json({ success: true, authenticated: false, user: null }); } diff --git a/backend/src/utils/tokenUtils.js b/backend/src/utils/tokenUtils.js index 5877d83..a9d2f12 100644 --- a/backend/src/utils/tokenUtils.js +++ b/backend/src/utils/tokenUtils.js @@ -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 }); }; diff --git a/frontend/src/context/AuthContext.jsx b/frontend/src/context/AuthContext.jsx index 1bd873f..12e7b34 100644 --- a/frontend/src/context/AuthContext.jsx +++ b/frontend/src/context/AuthContext.jsx @@ -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"); + } } 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); + } 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); + } toast.success("Account created successfully!"); return response; } catch (error) { @@ -94,6 +109,9 @@ export const AuthProvider = ({ children }) => { setUser(null); setIsAuthenticated(false); + // Clear token from localStorage + localStorage.removeItem("token"); + // Clear any cached data (optional, add if there's caching in localStorage) // sessionStorage.clear(); // Only if you're using sessionStorage