From 2b94459651f398fc2c48b2c298020510c3d813cb Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Tue, 27 Aug 2024 14:11:00 -0700 Subject: [PATCH] Only make a single client setup request. --- src/App.tsx | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 1904255..8b82747 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -15,15 +15,21 @@ interface IAuth { interface IAppState { auth: IAuth; + client: Client | null; } const App = () => { - const [state, setInternalState] = useState(null); + const [state, setInternalState] = useState({ + auth: { email: "", password: "" }, + client: null, + }); // When the user provides credentials const onLogin = (email: string, password: string) => { // Store the provided credentials for now - const state = { auth: { email: email, password: password } }; + state.auth.email = email; + state.auth.password = password; + state.client = null; setInternalState(state); localStorage.setItem("auth", JSON.stringify(state.auth)); doLogin(state.auth); @@ -36,38 +42,31 @@ const App = () => { const basic_auth = "Basic " + base64.encode(auth.email + ":" + auth.password); - let client = new Client({ + state.client = new Client({ accessToken: "fake token", httpHeaders: { Authorization: basic_auth }, sessionUrl: well_known_url, transport: new FetchTransport(fetch.bind(window)), }); - client + + state.client .fetchSession() .then((response) => console.log(response)) .catch((error) => console.error(error)); return; - /* - let headers = new Headers(); - headers.append("Authorization", basic_auth); - fetch(well_known_url, { - method: "GET", - headers: headers, - }) - .then(response => response.json()) - .then(data => { - console.log(data); - }) - .catch(error => console.error(error));*/ }; const loadAuth = () => { const data = localStorage.getItem("auth"); if (!data) return; const auth = JSON.parse(data); - setInternalState(auth); - doLogin(auth); + state.auth = auth; + if (state.client == null) { + console.log("NULL STATE.client"); + doLogin(state.auth); + return; + } }; useEffect(() => {