Skip to content

Commit

Permalink
solved
Browse files Browse the repository at this point in the history
  • Loading branch information
vanshulagarwal committed Jul 24, 2024
1 parent 0583bcf commit fdd03cc
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 49 deletions.
9 changes: 0 additions & 9 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 4 additions & 7 deletions client/src/Pages/Dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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'),
Expand Down
2 changes: 0 additions & 2 deletions client/src/Pages/Landing/Landing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 {
Expand Down
17 changes: 9 additions & 8 deletions client/src/Pages/Profile/Profile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -164,6 +164,7 @@ const Profile = () => {
}, {
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${localStorage.getItem('token')}`,
}
});

Expand Down Expand Up @@ -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 (
Expand Down
11 changes: 4 additions & 7 deletions client/src/components/Register/CompleteProfile.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -166,6 +162,7 @@ const CompleteProfile = () => {
}, {
headers: {
'Content-Type': 'multipart/form-data',
'Authorization': `Bearer ${localStorage.getItem('token')}`,
}
});

Expand Down
3 changes: 1 addition & 2 deletions client/src/components/Sidebar/Sidebar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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"
});
Expand Down
8 changes: 5 additions & 3 deletions client/src/hooks/useFetch.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
}
Expand All @@ -25,7 +27,7 @@ const useFetch = (url, makeCall = true,reload=0) => {
setLoading(false);
}
fetchData();
}, [url,reload])
}, [url, reload])

return { data, loading, error };
}
Expand Down
15 changes: 7 additions & 8 deletions client/src/hooks/usePostFetch.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import { makeRequest } from "./makeRequest";
import Cookies from 'js-cookie';

const usePostFetch = async (url, bodyData, makeCall = true) => {
try {
if (!makeCall) {
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 {
Expand Down
5 changes: 4 additions & 1 deletion client/src/hooks/usePutHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion server/utils/isLoggedIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit fdd03cc

Please sign in to comment.