Skip to content

Commit 4846bcb

Browse files
authored
Merge pull request #13 from foyzulkarim/feature/user-management
Implement User Authentication, Search, and Follow
2 parents 13706e4 + ea9236f commit 4846bcb

File tree

15 files changed

+623
-256
lines changed

15 files changed

+623
-256
lines changed

package-lock.json

Lines changed: 121 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@mui/material": "^5.14.12",
3232
"@vercel/analytics": "^1.2.2",
3333
"apexcharts": "^3.43.0",
34+
"axios": "^1.6.8",
3435
"date-fns": "^2.30.0",
3536
"history": "^5.3.0",
3637
"lodash": "^4.17.21",
@@ -76,5 +77,8 @@
7677
"extends": [
7778
"plugin:storybook/recommended"
7879
]
80+
},
81+
"volta": {
82+
"node": "20.12.2"
7983
}
8084
}

src/contexts/AuthContext.jsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ const AuthProvider = ({ children }) => {
2323

2424
if (!response.ok) {
2525
throw new Error('Profile fetch failed');
26+
} else {
27+
const data = await response.json();
28+
setAuthState(data);
29+
navigate('/login');
2630
}
27-
28-
const data = await response.json();
29-
setAuthState(data);
30-
navigate('/');
3131
} catch (error) {
32-
console.error('Error fetching profile:', error);
3332
clearAuthState();
34-
navigate('/', { replace: true });
33+
navigate('/login', { replace: true });
3534
}
3635
};
3736
console.log('fetchUserProfile:');
@@ -51,9 +50,13 @@ const AuthProvider = ({ children }) => {
5150
setUserProfile(null);
5251
};
5352

53+
const logout = async () => {
54+
window.location.href = `${apiUrl}/logout`;
55+
}
56+
5457
return (
5558
// eslint-disable-next-line react/jsx-no-constructed-context-values
56-
<AuthContext.Provider value={{ isAuthenticated, userProfile, setAuthState, clearAuthState }}>
59+
<AuthContext.Provider value={{ isAuthenticated, userProfile, setAuthState, clearAuthState, logout }}>
5760
{children}
5861
</AuthContext.Provider>
5962
);

src/layouts/dashboard/common/account-popover.jsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useState, useContext } from 'react';
2-
import { useNavigate } from 'react-router-dom';
32

43
import Box from '@mui/material/Box';
54
import Avatar from '@mui/material/Avatar';
@@ -34,10 +33,7 @@ const MENU_OPTIONS = [
3433

3534
export default function AccountPopover() {
3635
const [open, setOpen] = useState(null);
37-
const navigate = useNavigate();
38-
const { clearAuthState } = useContext(AuthContext);
39-
40-
const apiUrl = import.meta.env.VITE_API_URL;
36+
const { logout, userProfile } = useContext(AuthContext);
4137

4238
const handleOpen = (event) => {
4339
setOpen(event.currentTarget);
@@ -50,23 +46,17 @@ export default function AccountPopover() {
5046
const handleLogout = async () => {
5147
console.log('logout');
5248
try {
53-
const response = await fetch(`${apiUrl}/logout`, {
54-
method: 'POST', // Or appropriate method for your endpoint
55-
credentials: 'include',
56-
});
57-
58-
const res = await response.json();
59-
console.log('logout response:', res);
60-
61-
// Clear any additional user data
62-
clearAuthState();
63-
navigate('/login', { replace: true })
49+
await logout();
6450
} catch (error) {
6551
console.error('Logout error:', error);
6652
}
6753
setOpen(null);
6854
}
6955

56+
console.log('account:', { userProfile });
57+
const name = userProfile?.displayName || (userProfile?.username || account.displayName);
58+
const email = userProfile?.email || '';
59+
7060
return (
7161
<>
7262
<IconButton
@@ -82,15 +72,15 @@ export default function AccountPopover() {
8272
}}
8373
>
8474
<Avatar
85-
src={account.photoURL}
86-
alt={account.displayName}
75+
src={userProfile?.avatarUrl ?? account.photoURL}
76+
alt={name}
8777
sx={{
8878
width: 36,
8979
height: 36,
9080
border: (theme) => `solid 2px ${theme.palette.background.default}`,
9181
}}
9282
>
93-
{account.displayName.charAt(0).toUpperCase()}
83+
{name}
9484
</Avatar>
9585
</IconButton>
9686

@@ -111,10 +101,10 @@ export default function AccountPopover() {
111101
>
112102
<Box sx={{ my: 1.5, px: 2 }}>
113103
<Typography variant="subtitle2" noWrap>
114-
{account.displayName}
104+
{name}
115105
</Typography>
116106
<Typography variant="body2" sx={{ color: 'text.secondary' }} noWrap>
117-
{account.email}
107+
{email}
118108
</Typography>
119109
</Box>
120110

0 commit comments

Comments
 (0)