-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
145 additions
and
113 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import { BrowserRouter as Router, useNavigate, Link } from "react-router-dom"; | ||
import axiosInstance from "../api/axios" | ||
import { useUser } from "./UserContext"; | ||
export const Header = () => { | ||
const navigate = useNavigate(); | ||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
const { setUser } = useUser(); | ||
|
||
const checkLoginStatus = () => { | ||
const token = localStorage.getItem("access_token"); | ||
setIsLoggedIn(!!token); | ||
}; | ||
|
||
useEffect(() => { | ||
|
||
checkLoginStatus(); | ||
|
||
const handleStorageChange = () => { | ||
checkLoginStatus(); | ||
}; | ||
|
||
window.addEventListener("storage", handleStorageChange); | ||
return () => window.removeEventListener("storage", handleStorageChange); | ||
}, [checkLoginStatus,isLoggedIn]); | ||
|
||
const handleLogout = async () => { | ||
const refreshToken = localStorage.getItem("refresh_token"); | ||
try { | ||
if (refreshToken) { | ||
await axiosInstance.post("logout/", { refresh: refreshToken }); | ||
} | ||
} catch (error) { | ||
console.error("Error during logout:", error); | ||
} | ||
|
||
localStorage.removeItem("access_token"); | ||
localStorage.removeItem("refresh_token"); | ||
setIsLoggedIn(false); | ||
setUser(null); | ||
navigate("/"); | ||
}; | ||
|
||
return ( | ||
<div className="header"> | ||
<h1>Resume Builder</h1> | ||
{isLoggedIn && ( | ||
<div className="header-actions"> | ||
<Link to="/profile" className="profile-link"> | ||
Profile | ||
</Link> | ||
<Link to="/dashboard" className="profile-link"> | ||
Dashboard | ||
</Link> | ||
<button | ||
className="logout-button" | ||
onClick={handleLogout} | ||
> | ||
Logout | ||
</button> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
export default Header; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { createContext, useState, useContext } from 'react'; | ||
|
||
const UserContext = createContext(null); | ||
|
||
export const UserProvider = ({ children }) => { | ||
const [user, setUser] = useState(null); | ||
|
||
return ( | ||
<UserContext.Provider value={{ user, setUser }}> | ||
{children} | ||
</UserContext.Provider> | ||
); | ||
}; | ||
|
||
export const useUser = () => useContext(UserContext); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,12 @@ | ||
import React, { useEffect, useState } from "react"; | ||
import ReactDOM from "react-dom"; | ||
import { BrowserRouter as Router, useNavigate, Link } from "react-router-dom"; | ||
import "./index.css"; | ||
import AppRoutes from "./routes"; | ||
import axiosInstance from "./api/axios"; | ||
|
||
const Header = () => { | ||
const navigate = useNavigate(); | ||
const [isLoggedIn, setIsLoggedIn] = useState(false); | ||
|
||
useEffect(() => { | ||
const checkLoginStatus = () => { | ||
const token = localStorage.getItem("access_token"); | ||
setIsLoggedIn(!!token); | ||
}; | ||
|
||
checkLoginStatus(); | ||
|
||
// Optional: Add an event listener to detect token changes in localStorage | ||
const handleStorageChange = () => { | ||
checkLoginStatus(); | ||
}; | ||
|
||
window.addEventListener("storage", handleStorageChange); | ||
return () => window.removeEventListener("storage", handleStorageChange); | ||
}, []); | ||
|
||
const handleLogout = async () => { | ||
const refreshToken = localStorage.getItem("refresh_token"); | ||
try { | ||
if (refreshToken) { | ||
await axiosInstance.post("logout/", { refresh: refreshToken }); | ||
} | ||
} catch (error) { | ||
console.error("Error during logout:", error); | ||
} | ||
|
||
// Clear tokens and update state | ||
localStorage.removeItem("access_token"); | ||
localStorage.removeItem("refresh_token"); | ||
setIsLoggedIn(false); | ||
navigate("/"); | ||
}; | ||
|
||
return ( | ||
<div className="header"> | ||
<h1>Resume Builder</h1> | ||
{isLoggedIn && ( | ||
<div className="header-actions"> | ||
<Link to="/profile" className="profile-link"> | ||
Profile | ||
</Link> | ||
<button | ||
className="logout-button" | ||
onClick={handleLogout} | ||
> | ||
Logout | ||
</button> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
}; | ||
|
||
|
||
const App = () => ( | ||
<Router> | ||
<Header /> | ||
<AppRoutes /> | ||
</Router> | ||
); | ||
|
||
ReactDOM.render(<App />, document.getElementById("root")); | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import './index.css'; | ||
import App from './App'; | ||
import reportWebVitals from './reportWebVitals'; | ||
|
||
const root = ReactDOM.createRoot(document.getElementById('root')); | ||
root.render( | ||
<React.StrictMode> | ||
<App /> | ||
</React.StrictMode> | ||
); |
This file was deleted.
Oops, something went wrong.