Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrumilp12 committed Jun 9, 2024
1 parent a4f2c1b commit 43e40c5
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
5 changes: 3 additions & 2 deletions client/src/App.jsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,15 +10,15 @@ import { CssBaseline, Box } from '@mui/material';

function App() {
return (
<Router>
<UserProvider>
<Layout>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/auth" element={<AuthComponent />} />
<Route path="/user/profile/:userId" element={<UserProfile />} />
</Routes>
</Layout>
</Router>
</ UserProvider>
);
}

Expand Down
6 changes: 6 additions & 0 deletions client/src/Components/authComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
24 changes: 13 additions & 11 deletions client/src/Components/sideBar.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<Drawer
sx={{
width: drawerWidth,
flexShrink: 0,
mt: 8,
'& .MuiDrawer-paper': {
width: drawerWidth,
boxSizing: 'border-box',
position: 'relative'
},
}}
variant="permanent"
anchor="left"
>
<Typography variant="h6" sx={{ my: 2, mx: 2 }}>
Dashboard
</Typography>
<List>
<ListItem button>
<ListItemIcon><HomeIcon /></ListItemIcon>
Expand All @@ -32,10 +33,11 @@ function Sidebar() {
<ListItemIcon><PersonIcon /></ListItemIcon>
<ListItemText primary="Profile" />
</ListItem>
<ListItem button>
<ListItemIcon><SettingsIcon /></ListItemIcon>
<ListItemText primary="Settings" />
</ListItem>
<ListItem button onClick={logout}>
<ListItemIcon><ExitToAppIcon /></ListItemIcon>
<ListItemText primary="Logout" />
</ListItem>

</List>
</Drawer>
);
Expand Down
34 changes: 30 additions & 4 deletions client/src/Components/userContext.jsx
Original file line number Diff line number Diff line change
@@ -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 (
<UserContext.Provider value={{ user, setUser }}>
<UserContext.Provider value={{ user, setUser, logout }}>
{children}
</UserContext.Provider>
);
Expand Down
6 changes: 3 additions & 3 deletions client/src/main.jsx
Original file line number Diff line number Diff line change
@@ -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(
<React.StrictMode>
<UserProvider>
<BrowserRouter>
<App />
</UserProvider>
</BrowserRouter>
</React.StrictMode>,
)
4 changes: 3 additions & 1 deletion server/tools/azure_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
raise


0 comments on commit 43e40c5

Please sign in to comment.