Skip to content

Commit

Permalink
Fixed api endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Jshot117 committed Oct 16, 2024
1 parent 4bd130a commit 84fa1a9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
20 changes: 19 additions & 1 deletion BackEnd/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ app.get('/leaderboard', async (req, res) => {
console.log('A', usersSnapshot)
const leaderboard = [];
usersSnapshot.forEach(doc => {
leaderboard.push({ id: doc.id, ...doc.data() });
leaderboard.push({ id: doc.id, username: doc.data().username, points: doc.data().points });
});

res.status(200).json({ leaderboard });
Expand All @@ -32,6 +32,24 @@ app.get('/leaderboard', async (req, res) => {
}
});

app.post('/userInfo', async (req, res) => {
const userId = req.body.uid
console.log('User ID:', userId);

try {
const userDoc = await db.collection('users').doc(userId).get();
console.log(userDoc.data())

if (!userDoc.exists) {
res.status(404).json({ error: 'User not found' });
} else {
res.status(200).json({ username: userDoc.data().username, badges: userDoc.data().badges, points: userDoc.data().points, streak: userDoc.data().streaks });
}
} catch (error) {
console.error('Error fetching username:', error);
res.status(500).json({ error: 'Failed to fetch username' });
}
});
// Add a health check endpoint
app.get('/health', (req, res) => {
res.status(200).send('OK');
Expand Down
15 changes: 10 additions & 5 deletions FrontEnd/src/components/DashboardHeader.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ const DashboardHeader = ({ handleLogout }) => {
useEffect(() => {
const fetchUsername = async () => {
const user = auth.currentUser;
console.log("Current User:", user); // Log current user
console.log('Current user:', user);
if (user) {
try {
// Call the leaderboard API to fetch users
const response = await axios.get('https://jshot117-backend--3000.prod1.defang.dev/leaderboard');
const data = response.data.leaderboard;
const response = await axios.post('http://localhost:3000/userInfo', {
uid: user.uid
}, {
headers: {
'Content-Type': 'application/json'
}
});
const currentUser = await response.data;

// Find the current user in the leaderboard data
const currentUser = data.find(userData => userData.id === user.uid);

if (currentUser) {
setUsername(currentUser.username); // Set username if found
Expand All @@ -34,7 +39,7 @@ const DashboardHeader = ({ handleLogout }) => {
}
} else {
console.log("No user is logged in");
}
}https://github.com/Jshot117/HackHarvard
setLoading(false); // Update loading state once the data is fetched
};

Expand Down
3 changes: 2 additions & 1 deletion FrontEnd/src/components/LeaderBoard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export function LeaderBoard() {

const fetchLeaderboard = async () => {
try {
const response = await axios.get('https://jshot117-backend--3000.prod1.defang.dev/leaderboard');
console.log('http://localhost:3000/userInfo','https://jshot117-backend--3000.prod1.defang.dev/leaderboard')
const response = await axios.get('http://localhost:3000/leaderboard');
const data = response.data.leaderboard;
setCurrenUserPoints(data.find(user => user.id === auth.currentUser.uid).points);
const currentUser = data.find(user => user.id === auth.currentUser.uid);
Expand Down

0 comments on commit 84fa1a9

Please sign in to comment.