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 App from './App'
import { useState } from 'react' import { useState } from 'react'
function AuthHTTPWrapper() { function AuthHTTPWrapper() {
const [gotToken, setGotToken] = useState(false) const [token, setToken] = useState<string | undefined>(undefined)
let url: string | undefined = window.location.origin; let url: string | undefined = window.location.origin;
let audience: string | undefined; let audience: string | undefined;
@ -31,14 +31,14 @@ function AuthHTTPWrapper() {
useRefreshTokens={true} useRefreshTokens={true}
cacheLocation='localstorage' cacheLocation='localstorage'
> >
<AuthWrapper> <AuthWrapper setToken={setToken}>
<HTTPProvider setGotToken={setGotToken}> { token ?
{gotToken ? <HTTPProvider token={token}>
<App/> <App/>
:
<p>hi</p>
}
</HTTPProvider> </HTTPProvider>
:
<p>no token</p>
}
</AuthWrapper> </AuthWrapper>
</Auth0Provider> </Auth0Provider>
) )

View file

@ -1,19 +1,34 @@
import { useAuth0 } from "@auth0/auth0-react"; import { useAuth0 } from "@auth0/auth0-react";
import { PropsWithChildren, useEffect } from "react"; import { PropsWithChildren, useEffect, useState } from "react";
interface Props extends PropsWithChildren<any>{
setToken: React.Dispatch<React.SetStateAction<string | undefined>>
}
export default function AuthWrapper(props: PropsWithChildren<{}>) { export default function AuthWrapper(props: Props) {
const { children } = props; const { children, setToken } = props;
const { isLoading, loginWithRedirect, isAuthenticated } = useAuth0(); const { isLoading, loginWithRedirect, isAuthenticated, getAccessTokenSilently } = useAuth0();
useEffect(() => { useEffect(() => {
// console.log("Authenticated: ", isAuthenticated)
// console.log("Loading: ", isLoading)
if (!isAuthenticated && !isLoading) { if (!isAuthenticated && !isLoading) {
loginWithRedirect(); loginWithRedirect();
} }
}, [isAuthenticated, loginWithRedirect, isLoading]); }, [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 ( if (isAuthenticated) return (
children children
) )

View file

@ -6,7 +6,7 @@ import { createContext, PropsWithChildren, useContext, useEffect, useState } fro
// import GitlabProvider from "./gitlab"; // import GitlabProvider from "./gitlab";
import PondProvider from "./pond/pond"; import PondProvider from "./pond/pond";
// import SecurityProvider from "./security"; // import SecurityProvider from "./security";
import { useAuth0 } from "@auth0/auth0-react"; // import { useAuth0 } from "@auth0/auth0-react";
interface IHTTPContext { interface IHTTPContext {
get: <T>(url: string, spreadOptions?: AxiosRequestConfig) => Promise<AxiosResponse<T>>; get: <T>(url: string, spreadOptions?: AxiosRequestConfig) => Promise<AxiosResponse<T>>;
@ -25,24 +25,24 @@ interface IHTTPContext {
} }
interface Props extends PropsWithChildren<any>{ interface Props extends PropsWithChildren<any>{
setGotToken: React.Dispatch<React.SetStateAction<boolean>>; token: string;
} }
export const HTTPContext = createContext<IHTTPContext>({} as IHTTPContext); export const HTTPContext = createContext<IHTTPContext>({} as IHTTPContext);
export default function HTTPProvider(props: Props) { export default function HTTPProvider(props: Props) {
const { children, setGotToken } = props; const { children, token } = props;
const { isAuthenticated, /*token,*/ getAccessTokenSilently } = useAuth0(); // const { isAuthenticated, /*token,*/ getAccessTokenSilently } = useAuth0();
const [token, setToken] = useState(""); // const [token, setToken] = useState("");
useEffect(() => { // useEffect(() => {
if (isAuthenticated) getAccessTokenSilently().then(t => { // if (isAuthenticated) getAccessTokenSilently().then(t => {
console.log(t) // console.log(t)
setToken(t) // setToken(t)
}).finally(() => { // }).finally(() => {
setGotToken(true) // setGotToken(true)
}) // })
}, [setToken, isAuthenticated]) // }, [setToken, isAuthenticated])
const defaultOptions = (demo: boolean = false) => { const defaultOptions = (demo: boolean = false) => {
// if (demo || !isAuthenticated || !token) { // if (demo || !isAuthenticated || !token) {
@ -63,7 +63,7 @@ export default function HTTPProvider(props: Props) {
headers: { headers: {
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
"Content-Type": "application/json" "Content-Type": "application/json"
} },
}; };
return config; return config;
}; };