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 {
auth: IAuth;
client: Client | null;
}
const App = () => {
const [state, setInternalState] = useState<IAppState | null>(null);
const [state, setInternalState] = useState<IAppState>({
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(() => {