From fdd03cc61ebb53542d0619e98b1d8603793ee1ef Mon Sep 17 00:00:00 2001 From: Vanshul Agarwal Date: Thu, 25 Jul 2024 01:10:07 +0530 Subject: [PATCH] solved --- client/package-lock.json | 9 --------- client/package.json | 1 - client/src/Pages/Dashboard/Dashboard.jsx | 11 ++++------- client/src/Pages/Landing/Landing.jsx | 2 -- client/src/Pages/Profile/Profile.jsx | 17 +++++++++-------- .../src/components/Register/CompleteProfile.jsx | 11 ++++------- client/src/components/Sidebar/Sidebar.jsx | 3 +-- client/src/hooks/useFetch.js | 8 +++++--- client/src/hooks/usePostFetch.js | 15 +++++++-------- client/src/hooks/usePutHook.js | 5 ++++- server/utils/isLoggedIn.js | 2 +- 11 files changed, 35 insertions(+), 49 deletions(-) diff --git a/client/package-lock.json b/client/package-lock.json index 025508d..e998a0c 100644 --- a/client/package-lock.json +++ b/client/package-lock.json @@ -13,7 +13,6 @@ "chart.js": "^4.4.3", "chartjs-adapter-date-fns": "^3.0.0", "framer-motion": "^11.3.8", - "js-cookie": "^3.0.5", "react": "^18.3.1", "react-chartjs-2": "^5.2.0", "react-content-loader": "^7.0.2", @@ -3369,14 +3368,6 @@ "set-function-name": "^2.0.1" } }, - "node_modules/js-cookie": { - "version": "3.0.5", - "resolved": "https://registry.npmjs.org/js-cookie/-/js-cookie-3.0.5.tgz", - "integrity": "sha512-cEiJEAEoIbWfCZYKWhVwFuvPX1gETRYPw6LlaTKoxD3s2AkXzkCjnp6h0V77ozyqj0jakteJ4YqDJT830+lVGw==", - "engines": { - "node": ">=14" - } - }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/client/package.json b/client/package.json index e907570..968f43b 100644 --- a/client/package.json +++ b/client/package.json @@ -15,7 +15,6 @@ "chart.js": "^4.4.3", "chartjs-adapter-date-fns": "^3.0.0", "framer-motion": "^11.3.8", - "js-cookie": "^3.0.5", "react": "^18.3.1", "react-chartjs-2": "^5.2.0", "react-content-loader": "^7.0.2", diff --git a/client/src/Pages/Dashboard/Dashboard.jsx b/client/src/Pages/Dashboard/Dashboard.jsx index e76d92b..4b7b70d 100644 --- a/client/src/Pages/Dashboard/Dashboard.jsx +++ b/client/src/Pages/Dashboard/Dashboard.jsx @@ -24,7 +24,6 @@ import favourite from '../../assets/flaticon/favourite.gif' import chart from '../../assets/flaticon/chart.gif' import upcoming from '../../assets/flaticon/upcoming.gif' import Loader from '../../components/Loader/Loader'; -import Cookies from 'js-cookie'; const Dashboard = () => { const [searchParams, setSearchParams] = useSearchParams(); @@ -36,12 +35,10 @@ const Dashboard = () => { const user = data.user useEffect(() => { - if (searchParams.get('email') && searchParams.get('username')) { - // Cookies.set('token2', searchParams.get('token'), { - // ...searchParams.get('cookieOptions'), - // expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) - // }); - // setReload(prev => prev + 1); + if (searchParams.get('email') && searchParams.get('username') && searchParams.get('token')) { + localStorage.setItem('token', searchParams.get('token')) + + setReload(prev => prev + 1); dispatch(setAuth({ email: searchParams.get('email'), diff --git a/client/src/Pages/Landing/Landing.jsx b/client/src/Pages/Landing/Landing.jsx index dab1815..8eac7b0 100644 --- a/client/src/Pages/Landing/Landing.jsx +++ b/client/src/Pages/Landing/Landing.jsx @@ -25,7 +25,6 @@ import codeaz from '../../assets/codaez.png'; import Contest from '../../assets/Contest.jpeg'; import useFetch from '../../hooks/useFetch'; import Loader from '../../components/Loader/Loader'; -import Cookies from 'js-cookie'; const Landing = () => { const dispatch = useDispatch(); @@ -42,7 +41,6 @@ const Landing = () => { try { const data = await makeRequest.get('/logout', { withCredentials: true }); if (data.data) { - // Cookies.remove('token'); toast.success("Logged Out!", { position: "top-right" }); dispatch(removeAuth()); } else { diff --git a/client/src/Pages/Profile/Profile.jsx b/client/src/Pages/Profile/Profile.jsx index 4ebffe6..c40152b 100644 --- a/client/src/Pages/Profile/Profile.jsx +++ b/client/src/Pages/Profile/Profile.jsx @@ -38,7 +38,7 @@ const Profile = () => { const ownprofile = current_user?.username === username; const { data, loading, error } = useFetch(`/profile/${username}`, true, reload); - + useEffect(() => { if (ownprofile && data && data.user) { @@ -164,6 +164,7 @@ const Profile = () => { }, { headers: { 'Content-Type': 'multipart/form-data', + 'Authorization': `Bearer ${localStorage.getItem('token')}`, } }); @@ -273,16 +274,16 @@ const Profile = () => { } - useEffect(()=>{ - if(data && Object.keys(data).length>0 && !data.status ){ - toast.error("user not found" , { + useEffect(() => { + if (data && Object.keys(data).length > 0 && !data.status) { + toast.error("user not found", { position: "top-right" }) } - },[data]) - - - + }, [data]) + + + return ( diff --git a/client/src/components/Register/CompleteProfile.jsx b/client/src/components/Register/CompleteProfile.jsx index 81f8d36..87d614f 100644 --- a/client/src/components/Register/CompleteProfile.jsx +++ b/client/src/components/Register/CompleteProfile.jsx @@ -15,7 +15,6 @@ import { ClipLoader } from "react-spinners"; import { SiTicktick } from "react-icons/si"; import { RiErrorWarningLine } from "react-icons/ri"; import { IoIosArrowBack, IoIosArrowForward } from "react-icons/io"; -import Cookies from 'js-cookie'; const CompleteProfile = () => { @@ -50,12 +49,9 @@ const CompleteProfile = () => { useEffect(() => { - if (searchParams.get('email')) { - // Cookies.set('token2', searchParams.get('token'), { - // ...searchParams.get('cookieOptions'), - // expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) - // }); - + if (searchParams.get('email') && searchParams.get('token')) { + localStorage.setItem('token', searchParams.get('token')) + setUser({ email: searchParams.get('email'), name: searchParams.get('name'), @@ -166,6 +162,7 @@ const CompleteProfile = () => { }, { headers: { 'Content-Type': 'multipart/form-data', + 'Authorization': `Bearer ${localStorage.getItem('token')}`, } }); diff --git a/client/src/components/Sidebar/Sidebar.jsx b/client/src/components/Sidebar/Sidebar.jsx index c1e904e..2192ce1 100644 --- a/client/src/components/Sidebar/Sidebar.jsx +++ b/client/src/components/Sidebar/Sidebar.jsx @@ -9,7 +9,6 @@ import { removeAuth } from "../../redux/authReducer"; import { useDispatch, useSelector } from "react-redux"; import { toast } from "react-toastify"; import codaez from '../../assets/codaez.png' -import Cookies from 'js-cookie'; const Sidebar = () => { const dispatch = useDispatch(); @@ -25,7 +24,7 @@ const Sidebar = () => { if (data.data) { // console.log({ data: data.data }); - // Cookies.remove('token'); + localStorage.removeItem('token'); toast.success("Logged Out!", { position: "top-right" }); diff --git a/client/src/hooks/useFetch.js b/client/src/hooks/useFetch.js index 0ae2cde..ac4378b 100644 --- a/client/src/hooks/useFetch.js +++ b/client/src/hooks/useFetch.js @@ -1,7 +1,7 @@ import { useEffect, useState } from "react" import { makeRequest } from "./makeRequest"; -const useFetch = (url, makeCall = true,reload=0) => { +const useFetch = (url, makeCall = true, reload = 0) => { const [data, setData] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(false); @@ -14,7 +14,9 @@ const useFetch = (url, makeCall = true,reload=0) => { } setLoading(true); const resp = await makeRequest.get(url, { - withCredentials: true, + headers: { + 'Authorization': `Bearer ${localStorage.getItem('token')}`, + } }) setData(resp.data); } @@ -25,7 +27,7 @@ const useFetch = (url, makeCall = true,reload=0) => { setLoading(false); } fetchData(); - }, [url,reload]) + }, [url, reload]) return { data, loading, error }; } diff --git a/client/src/hooks/usePostFetch.js b/client/src/hooks/usePostFetch.js index f167a46..b2d2fe3 100644 --- a/client/src/hooks/usePostFetch.js +++ b/client/src/hooks/usePostFetch.js @@ -1,5 +1,4 @@ import { makeRequest } from "./makeRequest"; -import Cookies from 'js-cookie'; const usePostFetch = async (url, bodyData, makeCall = true) => { try { @@ -7,15 +6,15 @@ const usePostFetch = async (url, bodyData, makeCall = true) => { return {}; } const resp = await makeRequest.post(url, { ...bodyData }, { - withCredentials: true + headers: { + 'Authorization': `Bearer ${localStorage.getItem('token')}`, + } }) + console.log() if (resp.data) { - // if (resp.data.token) { - // Cookies.set('token2', resp.data.token, { - // ...resp.data.cookieOptions, - // expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000) - // }); - // } + if (resp.data.token) { + localStorage.setItem("token", resp.data.token); + } return { data: resp.data }; } else { diff --git a/client/src/hooks/usePutHook.js b/client/src/hooks/usePutHook.js index 179b6da..0df8909 100644 --- a/client/src/hooks/usePutHook.js +++ b/client/src/hooks/usePutHook.js @@ -5,7 +5,10 @@ const usePutHook = async (url, bodyData, options = {}) => { // pass options only when some extra options are also to be given e.g. while uploading image, option.header is required try { const data = await makeRequest.put(url, { ...bodyData }, { - withCredentials: true, + // withCredentials: true, + headers: { + 'Authorization': `Bearer ${localStorage.getItem('token')}`, + }, ...options }); // console.log(data) diff --git a/server/utils/isLoggedIn.js b/server/utils/isLoggedIn.js index 6b5061c..e3cf4e5 100644 --- a/server/utils/isLoggedIn.js +++ b/server/utils/isLoggedIn.js @@ -5,7 +5,7 @@ const ErrorHand = require('./errorHand.js'); module.exports.isLoggedIn = async (req, res, next) => { // const {token} = req.cookies; - const token = req.header("Authorization").replace("Bearer ", ""); + const token = req?.header("Authorization")?.replace("Bearer ", ""); if (!token) { return next(new ErrorHand("You need to log in first", 401)); }