getting token in auth wrapper and redirecting if it's no longer valid

This commit is contained in:
Carter 2024-10-25 15:16:02 -06:00
parent c2bfdd99aa
commit d8151f618a
3 changed files with 44 additions and 29 deletions

View file

@ -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: <T>(url: string, spreadOptions?: AxiosRequestConfig) => Promise<AxiosResponse<T>>;
@ -25,24 +25,24 @@ interface IHTTPContext {
}
interface Props extends PropsWithChildren<any>{
setGotToken: React.Dispatch<React.SetStateAction<boolean>>;
token: string;
}
export const HTTPContext = createContext<IHTTPContext>({} 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;
};