diff --git a/src/app/AuthHTTPWrapper.tsx b/src/app/AuthHTTPWrapper.tsx index 10ba8a1..17f7c0d 100644 --- a/src/app/AuthHTTPWrapper.tsx +++ b/src/app/AuthHTTPWrapper.tsx @@ -6,7 +6,7 @@ import App from './App' import { useState } from 'react' function AuthHTTPWrapper() { - const [gotToken, setGotToken] = useState(false) + const [token, setToken] = useState(undefined) let url: string | undefined = window.location.origin; let audience: string | undefined; @@ -31,14 +31,14 @@ function AuthHTTPWrapper() { useRefreshTokens={true} cacheLocation='localstorage' > - - - {gotToken ? + + { token ? + - : -

hi

- } -
+
+ : +

no token

+ }
) diff --git a/src/providers/auth.tsx b/src/providers/auth.tsx index 1ef11b5..6ef9ecf 100644 --- a/src/providers/auth.tsx +++ b/src/providers/auth.tsx @@ -1,19 +1,34 @@ import { useAuth0 } from "@auth0/auth0-react"; -import { PropsWithChildren, useEffect } from "react"; +import { PropsWithChildren, useEffect, useState } from "react"; +interface Props extends PropsWithChildren{ + setToken: React.Dispatch> +} -export default function AuthWrapper(props: PropsWithChildren<{}>) { - const { children } = props; - const { isLoading, loginWithRedirect, isAuthenticated } = useAuth0(); - +export default function AuthWrapper(props: Props) { + const { children, setToken } = props; + const { isLoading, loginWithRedirect, isAuthenticated, getAccessTokenSilently } = useAuth0(); + useEffect(() => { - // console.log("Authenticated: ", isAuthenticated) - // console.log("Loading: ", isLoading) if (!isAuthenticated && !isLoading) { loginWithRedirect(); } }, [isAuthenticated, loginWithRedirect, isLoading]); + useEffect(() => { + if (isAuthenticated) getAccessTokenSilently().then(t => { + if (t.length < 1) { + loginWithRedirect() + } else { + + } + setToken(t) + }).catch(() => { + // No token, go back to login + loginWithRedirect() + }) + }, [setToken, isAuthenticated]) + if (isAuthenticated) return ( children ) diff --git a/src/providers/http.tsx b/src/providers/http.tsx index 02d740b..1e47e0b 100644 --- a/src/providers/http.tsx +++ b/src/providers/http.tsx @@ -6,7 +6,7 @@ import { createContext, PropsWithChildren, useContext, useEffect, useState } fro // import GitlabProvider from "./gitlab"; import PondProvider from "./pond/pond"; // import SecurityProvider from "./security"; -import { useAuth0 } from "@auth0/auth0-react"; +// import { useAuth0 } from "@auth0/auth0-react"; interface IHTTPContext { get: (url: string, spreadOptions?: AxiosRequestConfig) => Promise>; @@ -25,24 +25,24 @@ interface IHTTPContext { } interface Props extends PropsWithChildren{ - setGotToken: React.Dispatch>; + token: string; } export const HTTPContext = createContext({} as IHTTPContext); export default function HTTPProvider(props: Props) { - const { children, setGotToken } = props; - const { isAuthenticated, /*token,*/ getAccessTokenSilently } = useAuth0(); - const [token, setToken] = useState(""); + const { children, token } = props; + // const { isAuthenticated, /*token,*/ getAccessTokenSilently } = useAuth0(); + // const [token, setToken] = useState(""); - useEffect(() => { - if (isAuthenticated) getAccessTokenSilently().then(t => { - console.log(t) - setToken(t) - }).finally(() => { - setGotToken(true) - }) - }, [setToken, isAuthenticated]) + // useEffect(() => { + // if (isAuthenticated) getAccessTokenSilently().then(t => { + // console.log(t) + // setToken(t) + // }).finally(() => { + // setGotToken(true) + // }) + // }, [setToken, isAuthenticated]) const defaultOptions = (demo: boolean = false) => { // if (demo || !isAuthenticated || !token) { @@ -63,7 +63,7 @@ export default function HTTPProvider(props: Props) { headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" - } + }, }; return config; };