Skip to content

Commit

Permalink
Merge pull request #74 from DFanso/api-connections-too
Browse files Browse the repository at this point in the history
Api connections too
  • Loading branch information
VidwaDeSeram authored Jan 7, 2024
2 parents 1da389d + 1bc2e8d commit 9f5fbde
Show file tree
Hide file tree
Showing 8 changed files with 518 additions and 114 deletions.
213 changes: 213 additions & 0 deletions admin/package-lock.json

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

3 changes: 3 additions & 0 deletions admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.4",
"fs": "^0.0.1-security",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-loader-spinner": "^6.1.6",
"react-router-dom": "^6.21.1",
"react-scripts": "5.0.1",
"socket.io": "^4.7.3",
"socket.io-client": "^4.7.3",
"sweetalert2": "^11.10.2",
"web-vitals": "^2.1.4"
},
Expand Down
52 changes: 27 additions & 25 deletions admin/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,44 +31,46 @@ import { UserContext } from "./pages/auth/UserContext"; // Import UserContext
import { UserProvider } from "./pages/auth/UserContext";

const App = () => {
const { updateUserData } = useContext(UserContext);
const { updateUserData, userData } = useContext(UserContext);
const [isLoggedIn, setIsLoggedIn] = useState(false);

useEffect(() => {
const checkUserLoggedIn = async () => {
const token = localStorage.getItem("admin-token");
if (token) {
try {
const profileResponse = await axios.get(
`${process.env.REACT_APP_API_PATH}/users/profile`,
{ headers: { Authorization: `Bearer ${token}` } }
);
if (!userData) {
const token = localStorage.getItem("admin-token");
if (token) {
try {
const profileResponse = await axios.get(
`${process.env.REACT_APP_API_PATH}/users/profile`,
{ headers: { Authorization: `Bearer ${token}` } }
);

if (profileResponse.data && profileResponse.data.type === "ADMIN") {
updateUserData(profileResponse.data);
setIsLoggedIn(true);
} else {
if (profileResponse.data && profileResponse.data.type === "ADMIN") {
updateUserData(profileResponse.data);
setIsLoggedIn(true);
} else {
Swal.fire({
title: "Access Denied",
text: "You are not authorized to access the admin panel.",
icon: "error",
confirmButtonText: "OK",
});
localStorage.removeItem("admin-token");
setIsLoggedIn(false);
}
} catch (error) {
console.error("Error fetching admin data:", error);
Swal.fire({
title: "Access Denied",
text: "You are not authorized to access the admin panel.",
title: "Error",
text: "An error occurred while fetching user data.",
icon: "error",
confirmButtonText: "OK",
});
localStorage.removeItem("admin-token");
setIsLoggedIn(false);
}
} catch (error) {
console.error("Error fetching admin data:", error);
Swal.fire({
title: "Error",
text: "An error occurred while fetching user data.",
icon: "error",
confirmButtonText: "OK",
});
} else {
setIsLoggedIn(false);
}
} else {
setIsLoggedIn(false);
}
};

Expand Down
Loading

0 comments on commit 9f5fbde

Please sign in to comment.