From 70d3b067425dadac01978352965b12550d63ee50 Mon Sep 17 00:00:00 2001 From: Matchu Date: Fri, 4 Sep 2020 05:59:35 -0700 Subject: [PATCH] basic items page, with user permissioning :) (the permissioning happens on the backend in the prev change! but yeah we send the auth token in the headers, so the backend knows who you are and whether to show you private data) (also it is just owned items not in any list!) --- src/app/App.js | 28 +++++++++-- src/app/HomePage.js | 22 +++------ src/app/ItemsPage.js | 73 ++++++++++++++++++++++++++++ src/app/apolloClient.js | 48 +++++++++++++++--- src/app/components/useCurrentUser.js | 19 ++++++++ 5 files changed, 163 insertions(+), 27 deletions(-) create mode 100644 src/app/ItemsPage.js create mode 100644 src/app/components/useCurrentUser.js 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 && (