diff --git a/src/app/App.js b/src/app/App.js index 2d5c4bc..aa08e73 100644 --- a/src/app/App.js +++ b/src/app/App.js @@ -5,11 +5,13 @@ import { CSSReset, ChakraProvider } from "@chakra-ui/core"; import defaultTheme from "@chakra-ui/theme"; import { BrowserRouter as Router, Switch, Route } from "react-router-dom"; import loadable from "@loadable/component"; +import { useAuth0 } from "@auth0/auth0-react"; -import apolloClient from "./apolloClient"; +import buildApolloClient from "./apolloClient"; -const WardrobePage = loadable(() => import("./WardrobePage")); +const ItemsPage = loadable(() => import("./ItemsPage")); const HomePage = loadable(() => import("./HomePage")); +const WardrobePage = loadable(() => import("./WardrobePage")); const theme = { ...defaultTheme, @@ -39,22 +41,40 @@ function App() { audience="https://impress-2020.openneo.net/api" scope="" > - + + + + - + ); } +function ApolloProviderWithAuth0({ children }) { + const auth0 = useAuth0(); + const auth0Ref = React.useRef(auth0); + + React.useEffect(() => { + auth0Ref.current = auth0; + }, [auth0]); + + const client = React.useMemo( + () => buildApolloClient(() => auth0Ref.current), + [] + ); + return {children}; +} + export default App; diff --git a/src/app/HomePage.js b/src/app/HomePage.js index 061b6ba..bcf7193 100644 --- a/src/app/HomePage.js +++ b/src/app/HomePage.js @@ -13,12 +13,13 @@ import { useToast, } from "@chakra-ui/core"; import { MoonIcon, SunIcon } from "@chakra-ui/icons"; -import { useHistory, useLocation } from "react-router-dom"; +import { Link, useHistory, useLocation } from "react-router-dom"; import { useLazyQuery } from "@apollo/client"; import { useAuth0 } from "@auth0/auth0-react"; import { Heading1, usePageTitle } from "./util"; import OutfitPreview from "./components/OutfitPreview"; +import useCurrentUser from "./components/useCurrentUser"; import HomepageSplashImg from "../images/homepage-splash.png"; import HomepageSplashImg2x from "../images/homepage-splash@2x.png"; @@ -76,32 +77,21 @@ function HomePage() { } function UserLoginLogout() { - const { - isLoading, - user, - isAuthenticated, - loginWithRedirect, - logout, - } = useAuth0(); + const { isLoading, isAuthenticated, loginWithRedirect, logout } = useAuth0(); + const { id, username } = useCurrentUser(); if (isLoading) { return null; } if (isAuthenticated) { - // NOTE: Users created correctly should have these attributes... but I'm - // coding defensively, because third-party integrations are always a - // bit of a thing, and I don't want failures to crash us! - const username = user["https://oauth.impress-2020.openneo.net/username"]; - const id = user.sub?.match(/^auth0\|impress-([0-9]+)$/)?.[1]; - return ( {username && Hi, {username}!} {id && (