Skip to content

Commit bb962c1

Browse files
committed
fix: errors
1 parent 14e4b3a commit bb962c1

File tree

4 files changed

+101
-11
lines changed

4 files changed

+101
-11
lines changed

frontend/src/components/Header/Header.css

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,53 @@ nav {
219219
color: white;
220220
}
221221

222+
.user-menu {
223+
display: flex;
224+
align-items: center;
225+
gap: 0.8rem;
226+
flex-shrink: 0;
227+
}
228+
229+
.user-avatar {
230+
width: 40px;
231+
height: 40px;
232+
border-radius: 50%;
233+
object-fit: cover;
234+
border: 2px solid var(--secondary-accent);
235+
}
236+
237+
.user-name {
238+
font-weight: 600;
239+
color: var(--secondary-accent);
240+
font-size: 16px;
241+
}
242+
243+
.btn__logout {
244+
display: flex;
245+
flex-direction: column;
246+
justify-content: center;
247+
align-items: center;
248+
padding: 10px 24px;
249+
gap: 10px;
250+
width: 147px;
251+
height: 64px;
252+
background: var(--secondary-accent);
253+
box-shadow: inset 2px 2px 6px #ffffff, inset -6px -5px 8px #102250;
254+
border-radius: 35px;
255+
border: none;
256+
color: white;
257+
font-size: 20px;
258+
font-weight: 600;
259+
transition: all 0.3s ease;
260+
flex-shrink: 0;
261+
cursor: pointer;
262+
}
263+
264+
.btn__logout:hover {
265+
transform: translateY(-2px);
266+
box-shadow: inset 2px 2px 8px #ffffff, inset -6px -5px 10px #102250;
267+
}
268+
222269
@media (min-width: 769px) and (max-width: 1024px) {
223270
.header {
224271
width: calc(100% - 2rem);

frontend/src/components/Header/Header.jsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ import dropdownArrow from '../../assets/landingPage/dropdownArrow.svg';
99
import hamburger from '../../assets/landingPage/hamburger.svg';
1010
import './Header.css';
1111
import {useState} from "react";
12+
import { useAuth } from '../../context/AuthContext';
1213

1314
export default function Header() {
1415

1516
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
1617
const [hamburgerOpen, setHamburgerOpen] = useState(false);
18+
const { user, logout, isAuthenticated } = useAuth();
19+
20+
const handleLogout = () => {
21+
logout();
22+
setHamburgerOpen(false);
23+
};
1724

1825
return (
1926
<header className="header">
@@ -63,7 +70,15 @@ export default function Header() {
6370
</nav>
6471
</div>
6572

66-
<Link to="/login" className="btn__login">Login</Link>
73+
{isAuthenticated ? (
74+
<div className="user-menu">
75+
<img src={user?.picture || '/default-avatar.png'} alt="Profile" className="user-avatar" />
76+
<span className="user-name">{user?.name}</span>
77+
<button onClick={handleLogout} className="btn__logout">Logout</button>
78+
</div>
79+
) : (
80+
<Link to="/login" className="btn__login">Login</Link>
81+
)}
6782
</div>
6883

6984
<div className="navbar">
@@ -104,7 +119,16 @@ export default function Header() {
104119
<li><NavLink to='/discussion' className="nav__link">Discussions</NavLink></li>
105120
</ul>
106121
</nav>
107-
<Link to="/login" className="btn__login">Login</Link>
122+
123+
{isAuthenticated ? (
124+
<div className="user-menu">
125+
<img src={user?.picture || '/default-avatar.png'} alt="Profile" className="user-avatar" />
126+
<span className="user-name">{user?.name}</span>
127+
<button onClick={logout} className="btn__logout">Logout</button>
128+
</div>
129+
) : (
130+
<Link to="/login" className="btn__login">Login</Link>
131+
)}
108132
</div>
109133
</header>
110134
)

frontend/src/context/AuthContext.jsx

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const AuthProvider = ({ children }) => {
1111
const [loading, setLoading] = useState(true);
1212
const [token, setToken] = useState(localStorage.getItem('authToken'));
1313

14-
const API_BASE = import.meta.env.VITE_API_BASE_URL;
14+
const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:4000';
1515

1616
useEffect(() => {
1717
const unsubscribe = onAuthStateChanged(auth, async (firebaseUser) => {
@@ -27,7 +27,17 @@ export const AuthProvider = ({ children }) => {
2727
});
2828

2929
if (response.ok) {
30-
setUser(firebaseUser);
30+
const data = await response.json();
31+
const userData = data.user || {
32+
uid: firebaseUser.uid,
33+
email: firebaseUser.email,
34+
name: firebaseUser.displayName || firebaseUser.email.split('@')[0],
35+
picture: firebaseUser.photoURL,
36+
emailVerified: firebaseUser.emailVerified,
37+
role: 'USER'
38+
};
39+
40+
setUser(userData);
3141
setToken(idToken);
3242
localStorage.setItem('authToken', idToken);
3343
} else {
@@ -37,11 +47,17 @@ export const AuthProvider = ({ children }) => {
3747
localStorage.removeItem('authToken');
3848
}
3949
} catch (error) {
40-
console.error('Auth error:', error);
41-
await signOut(auth);
42-
setUser(null);
43-
setToken(null);
44-
localStorage.removeItem('authToken');
50+
const userData = {
51+
uid: firebaseUser.uid,
52+
email: firebaseUser.email,
53+
name: firebaseUser.displayName || firebaseUser.email.split('@')[0],
54+
picture: firebaseUser.photoURL,
55+
emailVerified: firebaseUser.emailVerified,
56+
role: 'USER'
57+
};
58+
setUser(userData);
59+
setToken(idToken);
60+
localStorage.setItem('authToken', idToken);
4561
}
4662
} else {
4763
await signOut(auth);

frontend/src/pages/Home/Home.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Link } from "react-router-dom";
33
import Navbar from "../../components/Navbar";
44
import dropdown from "../../assets/home/dropdown.svg";
55
import dropdownArrow from "../../assets/home/dropdownArrow.svg";
6+
import { useAuth } from "../../context/AuthContext";
67
import "./Home.css";
78

89
export default function Home() {
@@ -12,6 +13,8 @@ export default function Home() {
1213
const [branchDropdown, setBranchDropdown] = useState(false);
1314

1415
const [branches, setBranches] = useState([]);
16+
const { user } = useAuth();
17+
1518
useEffect(() => {
1619
fetch("http://localhost:4000/branches")
1720
.then((r) => r.json())
@@ -21,7 +24,7 @@ export default function Home() {
2124
return (
2225
<div className="main">
2326
<div className="heading">
24-
<h1>Welcome Parth!</h1>
27+
<h1>Welcome {user?.name || 'Student'}!</h1>
2528
<p className="sub-heading">
2629
Select your <br />
2730
Semester and Branch.
@@ -68,7 +71,7 @@ export default function Home() {
6871
setSemDropdown(!semDropdown);
6972
}}
7073
>
71-
<p>3</p>
74+
<p>2</p>
7275
</li>
7376
<li
7477
className="sem-list__item"

0 commit comments

Comments
 (0)