From 43e40c5dd1d972a7893a83a1c1b2b0a7082864e7 Mon Sep 17 00:00:00 2001
From: DHRUMIL PATEL <123137675+dhrumilp12@users.noreply.github.com>
Date: Sat, 8 Jun 2024 20:52:15 -0400
Subject: [PATCH] ..
---
client/src/App.jsx | 5 ++--
client/src/Components/authComponent.jsx | 6 +++++
client/src/Components/sideBar.jsx | 24 +++++++++--------
client/src/Components/userContext.jsx | 34 ++++++++++++++++++++++---
client/src/main.jsx | 6 ++---
server/tools/azure_mongodb.py | 4 ++-
6 files changed, 58 insertions(+), 21 deletions(-)
diff --git a/client/src/App.jsx b/client/src/App.jsx
index 1ea60d81..8d30a63b 100644
--- a/client/src/App.jsx
+++ b/client/src/App.jsx
@@ -1,4 +1,5 @@
import React, { useState } from 'react';
+import { UserProvider } from './Components/userContext';
import { BrowserRouter as Router, Routes, Route, useLocation } from 'react-router-dom';
import Home from './Components/Home'; // Ensure this path is correct
import AuthComponent from './Components/authComponent';
@@ -9,7 +10,7 @@ import { CssBaseline, Box } from '@mui/material';
function App() {
return (
-
+
} />
@@ -17,7 +18,7 @@ function App() {
} />
-
+ UserProvider>
);
}
diff --git a/client/src/Components/authComponent.jsx b/client/src/Components/authComponent.jsx
index 9ae9c892..1b5db7c7 100644
--- a/client/src/Components/authComponent.jsx
+++ b/client/src/Components/authComponent.jsx
@@ -87,6 +87,8 @@ function AuthComponent() {
const response = await axios.post('/api/user/login', { username, password });
if (response && response.data) {
const userId = response.data.userId;
+ localStorage.setItem('token', response.data.access_token); // Ensure this is correctly saving the token
+ console.log('Token stored:', localStorage.getItem('token')); // Confirm the token is stored
setMessage('Login successful!');
setSeverity('success');
setIsAuthenticated(true);
@@ -121,6 +123,8 @@ function AuthComponent() {
});
if (response && response.data) {
const userId = response.data.userId;
+ localStorage.setItem('token', response.data.access_token); // Ensure this is correctly saving the token
+ console.log('Token stored:', localStorage.getItem('token')); // Confirm the token is stored
setMessage('User registered successfully!');
setSeverity('success');
setIsAuthenticated(true);
@@ -147,6 +151,8 @@ function AuthComponent() {
const response = await axios.post('/api/user/anonymous_signin');
if (response && response.data) {
const userId = null;
+ localStorage.setItem('token', response.data.access_token); // Ensure this is correctly saving the token
+ console.log('Token stored:', localStorage.getItem('token')); // Confirm the token is stored
setMessage('Anonymous sign-in successful!');
setSeverity('success');
setIsAuthenticated(true);
diff --git a/client/src/Components/sideBar.jsx b/client/src/Components/sideBar.jsx
index 43132112..ab2e1d99 100644
--- a/client/src/Components/sideBar.jsx
+++ b/client/src/Components/sideBar.jsx
@@ -1,28 +1,29 @@
-import React from 'react';
-import { Drawer, List, ListItem, ListItemIcon, ListItemText, Typography } from '@mui/material';
+import React, {useContext} from 'react';
+import { Drawer, List, ListItem, ListItemIcon, ListItemText} from '@mui/material';
import HomeIcon from '@mui/icons-material/Home';
import PersonIcon from '@mui/icons-material/Person';
-import SettingsIcon from '@mui/icons-material/Settings';
+import ExitToAppIcon from '@mui/icons-material/ExitToApp';
+import { UserContext } from './userContext';
-const drawerWidth = 240;
+const drawerWidth = 230;
function Sidebar() {
+ const { logout } = useContext(UserContext);
return (
-
- Dashboard
-
@@ -32,10 +33,11 @@ function Sidebar() {
-
-
-
-
+
+
+
+
+
);
diff --git a/client/src/Components/userContext.jsx b/client/src/Components/userContext.jsx
index 3adc878f..bdcd6300 100644
--- a/client/src/Components/userContext.jsx
+++ b/client/src/Components/userContext.jsx
@@ -1,12 +1,38 @@
-import React, { createContext, useState } from 'react';
-
+import React, { createContext, useState, useCallback } from 'react';
+import axios from 'axios';
+import { useNavigate } from 'react-router-dom';
export const UserContext = createContext(null);
export const UserProvider = ({ children }) => {
- const [user, setUser] = useState({ userId: null });
+ const [user, setUser] = useState(null);
+ const navigate = useNavigate();
+ const logout = useCallback(async () => {
+ try {
+ const token = localStorage.getItem('token');
+ console.log('Logging out with token:', token);
+ if (!token) {
+ console.error('No token available for logout');
+ return; // Exit the function if no token is available
+ }
+ const response = await axios.post('/api/user/logout', {}, {
+ headers: {
+ 'Authorization': `Bearer ${token}`
+ }
+ });
+ if (response.status === 200) {
+ setUser(null); // Clear user context
+ localStorage.removeItem('token'); // Clear token from local storage
+ navigate('/auth'); // Redirect after logout
+ } else {
+ throw new Error('Logout failed with status: ' + response.status);
+ }
+ } catch (error) {
+ console.error('Logout failed:', error.response?.data || error.message);
+ }
+ }, [navigate]);
return (
-
+
{children}
);
diff --git a/client/src/main.jsx b/client/src/main.jsx
index 693423dd..27fae8b1 100644
--- a/client/src/main.jsx
+++ b/client/src/main.jsx
@@ -1,12 +1,12 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'
-import { UserProvider } from './Components/userContext';
+import { BrowserRouter } from 'react-router-dom';
ReactDOM.createRoot(document.getElementById('root')).render(
-
+
-
+
,
)
\ No newline at end of file
diff --git a/server/tools/azure_mongodb.py b/server/tools/azure_mongodb.py
index a14a9ed8..bdbe4fb0 100644
--- a/server/tools/azure_mongodb.py
+++ b/server/tools/azure_mongodb.py
@@ -159,4 +159,6 @@ def get_user_mood_logs(user_id):
return list(db.mood_logs.find({"user_id": user_id}))
except pymongo.errors.PyMongoError as e:
logging.error(f"Error retrieving mood logs: {str(e)}")
- raise
\ No newline at end of file
+ raise
+
+
\ No newline at end of file