diff --git a/hotel-reservation-app/hotel-reservation-frontend/src/App.tsx b/hotel-reservation-app/hotel-reservation-frontend/src/App.tsx index 158bfa3..883e77d 100644 --- a/hotel-reservation-app/hotel-reservation-frontend/src/App.tsx +++ b/hotel-reservation-app/hotel-reservation-frontend/src/App.tsx @@ -14,6 +14,8 @@ import NotFound from "./pages/not_found"; import LandingPage from "./pages/landing_page"; import theme from "./theme"; import ErrorPage from "./pages/error"; +import Cookies from "js-cookie"; + export default function App() { const [signedIn, setSignedIn] = useState(false); @@ -25,6 +27,42 @@ export default function App() { }); const [isAuthLoading, setIsAuthLoading] = useState(false); + function getMappedUser(userInfo: any): User { + return { + email: userInfo?.email || "", + id: userInfo?.sub || "", + name: userInfo?.first_name + " " + userInfo?.last_name, + mobileNumber: userInfo?.mobile_number || "", + }; + } + useEffect(() => { + setIsAuthLoading(true); + if (Cookies.get("userinfo")) { + // We are here after a login + const userInfoCookie = Cookies.get("userinfo"); + sessionStorage.setItem("userInfo", userInfoCookie || ""); + Cookies.remove("userinfo"); + var userInfo = userInfoCookie ? JSON.parse(atob(userInfoCookie)) : {}; + setSignedIn(true); + setUser(getMappedUser(userInfo)); + } else if (sessionStorage.getItem("userInfo")) { + // We have already logged in + var userInfo = JSON.parse(atob(sessionStorage.getItem("userInfo")!)); + setSignedIn(true); + setUser(getMappedUser(userInfo)); + } else { + console.log("User is not signed in"); + if ( + window.location.pathname !== "/auth/login" && + window.location.pathname !== "/" + ) { + window.location.pathname = "/auth/login"; + } + } + setIsAuthLoading(false); + }, []); + + if (isAuthLoading) { return
User authenticating...
; } diff --git a/hotel-reservation-app/hotel-reservation-frontend/src/layout/AppBar.tsx b/hotel-reservation-app/hotel-reservation-frontend/src/layout/AppBar.tsx index b9f3958..473e176 100644 --- a/hotel-reservation-app/hotel-reservation-frontend/src/layout/AppBar.tsx +++ b/hotel-reservation-app/hotel-reservation-frontend/src/layout/AppBar.tsx @@ -10,6 +10,8 @@ import AutoAwesomeIcon from "@mui/icons-material/AutoAwesome"; import { Home } from "@mui/icons-material"; import { Button, Icon } from "@mui/material"; import { UserContext } from "../contexts/user"; +import Cookies from "js-cookie"; + function UserMenu() { const user = React.useContext(UserContext); @@ -48,7 +50,12 @@ function UserMenu() { open={Boolean(anchorElUser)} onClose={handleCloseUserMenu} > - (window.location.pathname = "/reservations")}> + { +sessionStorage.removeItem("userInfo"); +window.location.href = +`/auth/logout?session_hint=${Cookies.get('session_hint')}`; +}} +> diff --git a/hotel-reservation-app/hotel-reservation-frontend/src/pages/landing_page/index.tsx b/hotel-reservation-app/hotel-reservation-frontend/src/pages/landing_page/index.tsx index d9a607b..e30f403 100644 --- a/hotel-reservation-app/hotel-reservation-frontend/src/pages/landing_page/index.tsx +++ b/hotel-reservation-app/hotel-reservation-frontend/src/pages/landing_page/index.tsx @@ -24,7 +24,7 @@ export default function LandingPage() {