diff --git a/backend/src/app.js b/backend/src/app.js index 411061b..957f8e7 100644 --- a/backend/src/app.js +++ b/backend/src/app.js @@ -8,7 +8,6 @@ const rateLimit = require("express-rate-limit"); const dotenv = require('dotenv').config(); const session = require('express-session'); const mongoose = require('mongoose'); -const cookieParser = require('cookie-parser'); const passport = require('passport'); const useGoogleStrategy = require('./config/passport.config.js'); const useLocalStrategy = require('./config/passport.config.js'); @@ -24,7 +23,6 @@ mongoose.connect(process.env.MONGODB_URL, { useNewUrlParser: true, useUnifiedTop }).catch((error) => {console.log(error)}); -app.use(cookieParser()); const allowedOrigins = ['http://localhost:3000',"https://mycity-omega.vercel.app"]; app.use(cors({ diff --git a/backend/src/middlewares/jwtAuth.js b/backend/src/middlewares/jwtAuth.js deleted file mode 100644 index f3dfa23..0000000 --- a/backend/src/middlewares/jwtAuth.js +++ /dev/null @@ -1,23 +0,0 @@ - -const jwt = require('jsonwebtoken'); -const dotenv = require('dotenv').config(); - -const jwtAuth = (req, res, next) => { - const token = req.cookies.jwtToken; - if (!token) { - console.log("No token found") - } - else{ - try { - const decoded = jwt.verify(token, process.env.JWT_SECRET || ''); - req.user = decoded; - next(); - } catch (err) { - console.error(err); - } - } - -}; - - -module.exports = jwtAuth; \ No newline at end of file diff --git a/backend/src/routes/auth.js b/backend/src/routes/auth.js index 53bbb1c..d29f240 100644 --- a/backend/src/routes/auth.js +++ b/backend/src/routes/auth.js @@ -14,14 +14,7 @@ authRouter.get('/google/callback',passport.authenticate('google', { failureRedir process.env.JWT_SECRET, { expiresIn: "1h" }, ); - res.cookie('jwtToken', token, { - domain: process.env.FRONTEND_URL, - secure: true, - sameSite: 'none', - httpOnly: true, - path: '/' - }); - res.redirect(process.env.FRONTEND_URL); + res.redirect(`${process.env.FRONTEND_URL}?token=${token}`); } ); diff --git a/backend/src/routes/userRoutes.js b/backend/src/routes/userRoutes.js index d755e23..80e66b3 100644 --- a/backend/src/routes/userRoutes.js +++ b/backend/src/routes/userRoutes.js @@ -6,13 +6,13 @@ const passport = require("passport"); const isAuthenticated = require("../middlewares/isAuthenticated"); const jwt = require('jsonwebtoken'); const router = express.Router(); -const jwtAuth = require("../middlewares/jwtAuth"); -router.get("/user-details", jwtAuth, (req, res) => { +router.post("/user-details", (req, res) => { console.log("Inside user-details"); - if (req.user) { - console.log(req.user); - res.json(req.user); + console.log(req.body); + if(req.body.jwtToken){ + const decoded = jwt.verify(req.body.jwtToken, process.env.JWT_SECRET || ''); + res.json(decoded); } else { res.status(404).json({ message: "User not found" }); } diff --git a/frontend/src/App.js b/frontend/src/App.js index 52291b8..d54f9cf 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -1,7 +1,7 @@ import "./App.css"; import { ChakraProvider } from "@chakra-ui/react"; import { useEffect, useState } from "react"; -import { Routes, Route } from 'react-router-dom'; +import { Routes, Route} from 'react-router-dom'; import Login from "./components/Login"; import Navbar from "./components/Nav"; import Register from "./pages/Register"; @@ -14,17 +14,23 @@ import UserContext from './components/UserContext'; function App() { + const [user, setUser] = useState(null); useEffect(() => { - userService.user_details() + const jwtToken = localStorage.getItem("jwtToken"); + if (jwtToken) { + userService.user_details(jwtToken) .then((res) => { setUser(res.data.user); }) .catch(() => { setUser(null); }); - }, []); + + } else { + setUser(null); + } }, []); return ( diff --git a/frontend/src/components/Login.jsx b/frontend/src/components/Login.jsx index 579ccc5..6c988f3 100644 --- a/frontend/src/components/Login.jsx +++ b/frontend/src/components/Login.jsx @@ -53,7 +53,7 @@ function Login() { if (res.status !== 401 && res.status !== 500) { navigate('/'); window.location.reload(); - document.cookie = `jwtToken=${res.data.token}; path=/;`; // set JWT token to a cookie + localStorage.setItem('jwtToken', res.data.token); // set JWT token to local storage setIsLoading(false); setIsFormError(false); } else{ diff --git a/frontend/src/components/Logout.jsx b/frontend/src/components/Logout.jsx index 192532e..42bfc88 100644 --- a/frontend/src/components/Logout.jsx +++ b/frontend/src/components/Logout.jsx @@ -9,9 +9,8 @@ function Logout() { const handleLogout = () => { - // Clear the JWT token from the cookie - document.cookie = 'jwtToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; - // Navigate to the home page and reload + // Clear the JWT token from the local storage + localStorage.removeItem('jwtToken'); navigate('/'); window.location.reload(); }; diff --git a/frontend/src/pages/FinishRegister.jsx b/frontend/src/pages/FinishRegister.jsx index 83440fc..7c92714 100644 --- a/frontend/src/pages/FinishRegister.jsx +++ b/frontend/src/pages/FinishRegister.jsx @@ -19,7 +19,7 @@ function FinishRegister(props) { const [registerUser, setRegisterUser] = useState({}); const location = useLocation(); - const [userFromRegister, setUserFromRegister] = useState(location.state ? location.state.user : {}); + const [userFromRegister, setUserFromRegister] = useState(location.state ? location.state.user : null); useEffect(() => { @@ -28,14 +28,12 @@ function FinishRegister(props) { }).catch((err) => { console.log(err); }) - if (!registerUser.firstName && !registerUser.lastName && !registerUser.email && !registerUser.password) { - + if (userFromRegister) { setRegisterUser(userFromRegister); } else if (user) { setRegisterUser(user); } - } }, [user]); diff --git a/frontend/src/pages/Homepage.jsx b/frontend/src/pages/Homepage.jsx index 3250e79..d849bb5 100644 --- a/frontend/src/pages/Homepage.jsx +++ b/frontend/src/pages/Homepage.jsx @@ -3,7 +3,6 @@ import HomepageFeatureTab from '../components/HomepageFeatureTab' import { useNavigate } from 'react-router-dom'; import { useEffect,useContext } from 'react'; import UserContext from '../components/UserContext'; -import userService from '../services/userService'; function Homepage() { @@ -12,12 +11,22 @@ function Homepage() { const {user,setUser} = useContext(UserContext); useEffect(() => { + + const urlParams = new URLSearchParams(window.location.search); + + if (urlParams.has('token')) { + const token = urlParams.get('token'); + localStorage.setItem('jwtToken', token); + navigate('/') + } + else { if (user && user.city === '-') { navigate('/register/2'); } else { return; } - }); + } +}, [user]); const handleGetStarted = () => { diff --git a/frontend/src/services/userService.js b/frontend/src/services/userService.js index b1de4d2..e2c39ed 100644 --- a/frontend/src/services/userService.js +++ b/frontend/src/services/userService.js @@ -28,8 +28,8 @@ function logout() { return api.get(`${API_URL}/logout`,{withCredentials:true}); } -function user_details() { - return api.get(`${API_URL}/user-details`,{withCredentials:true}); +function user_details(jwtToken) { + return api.post(`${API_URL}/user-details`, { jwtToken }); }