Skip to content
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

FIX: data fetch request is send to backend without user logedin causes error is solved #258

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 12 additions & 27 deletions client/src/Context/UserContext.jsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { createContext, useEffect, useState } from "react";
import axios from "axios";
import { Loader } from "../components/Loader/Loader";
import { useNavigate } from "react-router-dom";

export const UserContext = createContext();

export const UserContextProvider = ({ children }) => {
const [user, setUser] = useState(null);
const navigate = useNavigate();
const [loading, setLoading] = useState(true);

const token = localStorage.getItem("token");

if (!token) {
navigate("/login");
}

const config = {
headers: {
Authorization: `Bearer ${token}`,
Expand All @@ -24,28 +16,21 @@ export const UserContextProvider = ({ children }) => {
};

const getUser = async () => {
try {
const res = await axios.get(
`${import.meta.env.VITE_BASE_URL}/user/get`,
config
);
console.log(res.data);
if (res.data.isverified === true) {
setUser(res.data);
if (token) {
try {
const res = await axios.get(
`${import.meta.env.VITE_BASE_URL}/user/get`,
config
);
console.log(res.data);
if (res.data.isverified === true) {
setUser(res.data);
}
} catch (error) {
console.log(error);
}
} catch (error) {
console.log(error);
} finally {
setLoading(false);
}
};
useEffect(() => {
getUser();
}, []);

if (loading) {
return <Loader />;
}

return (
<UserContext.Provider value={{ user, setUser, getUser }}>
Expand Down
55 changes: 29 additions & 26 deletions client/src/components/Dashboard/Dashboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,35 @@ const Dashboard = () => {
if (!token) {
navigate("/login");
}
axios
.get(`${import.meta.env.VITE_BASE_URL}/user/get`, {
headers: {
authorization: `Bearer ${token}`,
},
})
.then((res) => {
console.log(res.data);
if (!res.data.isverified) {
navigate("/verifyotp");
} else {
setUser(res.data);
}
setLoading(false);
})
.catch((err) => {
console.log(err.response.data.message);
if (
err.response.data.message === "Unauthorized" ||
err.response.data.message === "Failed to verify token" ||
err.response.data.message === "User not found"
) {
localStorage.removeItem("token");
navigate("/login");
}
});

if (token) {
axios
.get(`${import.meta.env.VITE_BASE_URL}/user/get`, {
headers: {
authorization: `Bearer ${token}`,
},
})
.then((res) => {
console.log(res.data);
if (!res.data.isverified) {
navigate("/verifyotp");
} else {
setUser(res.data);
}
setLoading(false);
})
.catch((err) => {
console.log(err.response.data.message);
if (
err.response.data.message === "Unauthorized" ||
err.response.data.message === "Failed to verify token" ||
err.response.data.message === "User not found"
) {
localStorage.removeItem("token");
navigate("/login");
}
});
}
}, []);

if (loading) {
Expand Down
7 changes: 3 additions & 4 deletions client/src/components/Footer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const Footer = () => {
Contact
</Link>
{lang[langKey].PrivacyPolicy}
</a>
</li>
<li>
<a href="/licensing" className="hover:underline me-4 md:me-6">
Expand All @@ -62,10 +61,10 @@ const Footer = () => {
</li>
</ul>
</div>
<hr class="my-6 border-gray-200 sm:mx-auto dark:border-gray-700 lg:my-8" />
<span class="block text-sm text-gray-500 sm:text-center dark:text-gray-400">
<hr className="my-6 border-gray-200 sm:mx-auto dark:border-gray-700 lg:my-8" />
<span className="block text-sm text-gray-500 sm:text-center dark:text-gray-400">
© {new Date().getFullYear()}
<Link to="/" class="hover:underline">
<Link to="/" className="hover:underline">
ExamTime
</Link>
. All Rights Reserved.
Expand Down
59 changes: 34 additions & 25 deletions client/src/components/Navbar/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Badge, Button } from "@material-tailwind/react";
import { UserContext } from "../../Context/UserContext";
import axios from "axios";
import { useLocation, useNavigate } from "react-router";
import Login from "../Login/Login.jsx";
import { Link } from "react-router-dom";
import { Bounce, toast } from "react-toastify";

function classNames(...classes) {
return classes.filter(Boolean).join(" ");
Expand Down Expand Up @@ -46,24 +46,27 @@ const Navbar = () => {
},
withCredentials: true,
};
axios
.get(`${import.meta.env.VITE_BASE_URL}/request`, config)
.then((res) => {
setRequests(res.data);
})
.catch((error) => {
toast.error("An error occurred", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: false,
pauseOnHover: false,
draggable: false,
progress: undefined,
theme: "light",
transition: Bounce,

if (token) {
axios
.get(`${import.meta.env.VITE_BASE_URL}/request`, config)
.then((res) => {
setRequests(res.data);
})
.catch((error) => {
toast.error("An error occurred", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: false,
pauseOnHover: false,
draggable: false,
progress: undefined,
theme: "light",
transition: Bounce,
});
});
});
}
} catch (error) {
console.log(error);
}
Expand All @@ -80,7 +83,7 @@ const Navbar = () => {
} else if (loc === "/question") {
setRequestNav(false);
setUploadNav(false);
setQuestionNav(true)
setQuestionNav(true);
} else if (loc === "/leaderboard") {
setleaderBoardNav(true);
setRequestNav(false);
Expand Down Expand Up @@ -133,12 +136,19 @@ const Navbar = () => {
d="M9 8.25H7.5a2.25 2.25 0 00-2.25 2.25v9a2.25 2.25 0 002.25 2.25h9a2.25 2.25 0 002.25-2.25v-9a2.25 2.25 0 00-2.25-2.25H15m0-3l-3-3m0 0l-3 3m3-3V15"
/>
</svg>
) : item.name === "Ask a Question" ? (
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" class="bi bi-question-circle" viewBox="0 0 16 16">
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16"/>
<path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286m1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94"/>
) : item.name === "Ask a Question" ? (
<svg
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
fill="currentColor"
className="bi bi-question-circle"
viewBox="0 0 16 16"
>
<path d="M8 15A7 7 0 1 1 8 1a7 7 0 0 1 0 14m0 1A8 8 0 1 0 8 0a8 8 0 0 0 0 16" />
<path d="M5.255 5.786a.237.237 0 0 0 .241.247h.825c.138 0 .248-.113.266-.25.09-.656.54-1.134 1.342-1.134.686 0 1.314.343 1.314 1.168 0 .635-.374.927-.965 1.371-.673.489-1.206 1.06-1.168 1.987l.003.217a.25.25 0 0 0 .25.246h.811a.25.25 0 0 0 .25-.25v-.105c0-.718.273-.927 1.01-1.486.609-.463 1.244-.977 1.244-2.056 0-1.511-1.276-2.241-2.673-2.241-1.267 0-2.655.59-2.75 2.286m1.557 5.763c0 .533.425.927 1.01.927.609 0 1.028-.394 1.028-.927 0-.552-.42-.94-1.029-.94-.584 0-1.009.388-1.009.94" />
</svg>
) :item.name === "Leaderboard" ? (
) : item.name === "Leaderboard" ? (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
Expand Down Expand Up @@ -178,7 +188,6 @@ const Navbar = () => {
</div>
</div>
</div>

</div>
<div className="absolute inset-y-0 right-0 flex gap-3 items-center pr-2 sm:static sm:inset-auto sm:ml-6 sm:pr-0">
<Menu as="div" className="relative ml-3">
Expand Down