diff --git a/src/components/AppBar/index.jsx b/src/components/AppBar/index.jsx index 13a403e..56e8216 100644 --- a/src/components/AppBar/index.jsx +++ b/src/components/AppBar/index.jsx @@ -8,13 +8,19 @@ import Brightness7Icon from "@mui/icons-material/Brightness7"; import MuiAppBar from "@mui/material/AppBar"; import Toolbar from "@mui/material/Toolbar"; import Typography from "@mui/material/Typography"; +import Button from "@mui/material/Button"; +import { useCookies } from "react-cookie"; + +import { githubLogin } from "../../lib/teuthologyAPI" + const PREFIX = 'index'; const classes = { appBar: `${PREFIX}-appBar`, title: `${PREFIX}-title`, - toolbarIcon: `${PREFIX}-toolbarIcon` + toolbarIcon: `${PREFIX}-toolbarIcon`, + loginButton: `${PREFIX}-loginButton`, }; const StyledMuiAppBar = styled(MuiAppBar)(( @@ -40,11 +46,43 @@ const StyledMuiAppBar = styled(MuiAppBar)(( alignItems: "center", justifyContent: "flex-end", padding: "0 8px", + }, + + [`& .${classes.loginButton}`]: { + // TODO: add theme + marginLeft: "12px", } + })); export default function AppBar(props) { const theme = useTheme(); + const [cookies, setCookie, removeCookie] = useCookies(["pulpitosession"]); + + + const getCookieData = () => { + // ISO-8859-1 octal string -> char string + const authcookies = (cookies["pulpitosession"].replace(/\\073/g, ';')) + + if (authcookies){ + let cookie_dict = {} + let cookies_ = authcookies.split(";") + cookies_.map((cookie) => { + let [key, value] = cookie.split("="); + cookie_dict[key.trim()] = value.trim(); + }) + return cookie_dict + } + } + + const getUsername = () => { + const cookieData = getCookieData(); + return cookieData["username"]; + } + + const login = () => { + githubLogin(); + } return ( @@ -78,6 +116,14 @@ export default function AppBar(props) { Pulpito +
+ { cookies["pulpitosession"] + ? + Hey, {getUsername()} ! + + : + } +
{props.darkMode ? : } diff --git a/src/index.tsx b/src/index.tsx index 359b55b..c777a0c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,6 +4,7 @@ import { BrowserRouter as Router } from "react-router-dom"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { QueryParamProvider } from "use-query-params"; import { ReactRouter6Adapter } from "use-query-params/adapters/react-router-6"; +import { CookiesProvider } from "react-cookie"; import axios from "axios"; import CssBaseline from "@mui/material/CssBaseline"; import { ReactQueryDevtools } from "@tanstack/react-query-devtools"; @@ -78,15 +79,17 @@ export default function Root() { - - - - - - - - - + + + + + + + + + + + diff --git a/src/lib/teuthologyAPI.ts b/src/lib/teuthologyAPI.ts new file mode 100644 index 0000000..04aee3f --- /dev/null +++ b/src/lib/teuthologyAPI.ts @@ -0,0 +1,12 @@ +import { useQuery } from "@tanstack/react-query"; + +const TEUTHOLOGY_API_SERVER = + import.meta.env.VITE_TEUTHOLOGY_API || ""; + +function githubLogin() { + window.location.replace(`${TEUTHOLOGY_API_SERVER}/login`); +} + +export { + githubLogin, +} \ No newline at end of file