Skip to content

Commit

Permalink
Merge pull request #29 from DFanso/api-connections
Browse files Browse the repository at this point in the history
auth bug fix
  • Loading branch information
VidwaDeSeram authored Jan 3, 2024
2 parents 36eac24 + 668877a commit 03871f8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
7 changes: 4 additions & 3 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ function App() {
const fetchUserData = async () => {
const token = localStorage.getItem("token");
if (token) {
console.log(token);
try {
const profileResponse = await axios.get(
`${process.env.REACT_APP_API_PATH}/users/profile`,
Expand All @@ -51,21 +52,21 @@ function App() {
},
}
);
setUserData(profileResponse.data);

// Assuming profileResponse.data contains the user object
if (profileResponse.data) {
setUserData(profileResponse.data); // Update with actual data
dispatch(login());
setIsUserLoggedIn(true); // Set logged in state to true
}
} catch (error) {
console.error("Error fetching user data:", error);
// Handle error appropriately
}
}
};

fetchUserData();
}, [dispatch, setUserData]);
}, []);

return (
<LoadingProvider>
Expand Down
40 changes: 35 additions & 5 deletions client/src/components/UserInformationComponent.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,44 @@
import React, { useContext } from "react";
import { UserContext } from "./UserContext";
import React, { useEffect, useState, useContext } from "react";
import { useSelector } from "react-redux";
import axios from "axios";
import "./css/UserInformationComponent.css";

function UserInformationComponent() {
const { userData } = useContext(UserContext);
console.log(userData);
const [userData, setUserData] = useState({});
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const isLoggedIn = useSelector((state) => state.auth.isLoggedIn);

useEffect(() => {
const fetchUserData = async () => {
const token = localStorage.getItem("token");
if (isLoggedIn && token) {
setLoading(true);
try {
const response = await axios.get(
`${process.env.REACT_APP_API_PATH}/users/profile`,
{ headers: { Authorization: `Bearer ${token}` } }
);
setUserData(response.data);
} catch (error) {
console.error("Error fetching user data:", error);
setError("Failed to fetch user data.");
} finally {
setLoading(false);
}
}
};

fetchUserData();
}, [isLoggedIn]);

if (loading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;

return (
<div className="user-information">
<form>
<h1>user profile</h1>
<h1>User Profile</h1>
<div className="form-group">
<label htmlFor="firstName">First Name</label>
<p>{userData.firstName || "First name not set"}</p>
Expand Down

0 comments on commit 03871f8

Please sign in to comment.