Only make a single client setup request.

This commit is contained in:
Eli Ribble 2024-08-27 14:11:00 -07:00
parent 05779c0b64
commit 2b94459651
1 changed files with 17 additions and 18 deletions

View File

@ -15,15 +15,21 @@ interface IAuth {
interface IAppState { interface IAppState {
auth: IAuth; auth: IAuth;
client: Client | null;
} }
const App = () => { const App = () => {
const [state, setInternalState] = useState<IAppState | null>(null); const [state, setInternalState] = useState<IAppState>({
auth: { email: "", password: "" },
client: null,
});
// When the user provides credentials // When the user provides credentials
const onLogin = (email: string, password: string) => { const onLogin = (email: string, password: string) => {
// Store the provided credentials for now // 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); setInternalState(state);
localStorage.setItem("auth", JSON.stringify(state.auth)); localStorage.setItem("auth", JSON.stringify(state.auth));
doLogin(state.auth); doLogin(state.auth);
@ -36,38 +42,31 @@ const App = () => {
const basic_auth = const basic_auth =
"Basic " + base64.encode(auth.email + ":" + auth.password); "Basic " + base64.encode(auth.email + ":" + auth.password);
let client = new Client({ state.client = new Client({
accessToken: "fake token", accessToken: "fake token",
httpHeaders: { Authorization: basic_auth }, httpHeaders: { Authorization: basic_auth },
sessionUrl: well_known_url, sessionUrl: well_known_url,
transport: new FetchTransport(fetch.bind(window)), transport: new FetchTransport(fetch.bind(window)),
}); });
client
state.client
.fetchSession() .fetchSession()
.then((response) => console.log(response)) .then((response) => console.log(response))
.catch((error) => console.error(error)); .catch((error) => console.error(error));
return; 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 loadAuth = () => {
const data = localStorage.getItem("auth"); const data = localStorage.getItem("auth");
if (!data) return; if (!data) return;
const auth = JSON.parse(data); const auth = JSON.parse(data);
setInternalState(auth); state.auth = auth;
doLogin(auth); if (state.client == null) {
console.log("NULL STATE.client");
doLogin(state.auth);
return;
}
}; };
useEffect(() => { useEffect(() => {