2024-08-27 09:28:24 -07:00
|
|
|
import "./App.css";
|
2024-08-27 09:39:28 -07:00
|
|
|
import "bootstrap/dist/css/bootstrap.min.css";
|
2024-08-27 10:48:49 -07:00
|
|
|
import * as base64 from "base-64";
|
2024-08-27 14:00:30 -07:00
|
|
|
import { Client } from "jmap-client-ts";
|
|
|
|
import { FetchTransport } from "jmap-client-ts/lib/utils/fetch-transport";
|
2024-08-26 16:07:43 -07:00
|
|
|
|
2024-08-27 09:28:24 -07:00
|
|
|
//import MailboxList from "./Mailbox";
|
|
|
|
import AuthModal from "./AuthModal";
|
2024-08-27 09:39:28 -07:00
|
|
|
import React, { useEffect, useState } from "react";
|
2024-08-27 09:28:24 -07:00
|
|
|
|
|
|
|
interface IAuth {
|
2024-08-27 11:11:54 -07:00
|
|
|
email: string;
|
2024-08-27 09:39:28 -07:00
|
|
|
password: string;
|
2024-08-27 08:23:58 -07:00
|
|
|
}
|
|
|
|
|
2024-08-27 14:00:30 -07:00
|
|
|
interface IAppState {
|
|
|
|
auth: IAuth;
|
2024-08-27 14:11:00 -07:00
|
|
|
client: Client | null;
|
2024-08-27 14:00:30 -07:00
|
|
|
}
|
|
|
|
|
2024-08-27 08:23:58 -07:00
|
|
|
const App = () => {
|
2024-08-27 14:11:00 -07:00
|
|
|
const [state, setInternalState] = useState<IAppState>({
|
|
|
|
auth: { email: "", password: "" },
|
|
|
|
client: null,
|
|
|
|
});
|
2024-08-27 09:39:28 -07:00
|
|
|
|
2024-08-27 11:11:54 -07:00
|
|
|
// When the user provides credentials
|
|
|
|
const onLogin = (email: string, password: string) => {
|
|
|
|
// Store the provided credentials for now
|
2024-08-27 14:11:00 -07:00
|
|
|
state.auth.email = email;
|
|
|
|
state.auth.password = password;
|
|
|
|
state.client = null;
|
2024-08-27 14:00:30 -07:00
|
|
|
setInternalState(state);
|
|
|
|
localStorage.setItem("auth", JSON.stringify(state.auth));
|
|
|
|
doLogin(state.auth);
|
2024-08-27 11:11:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Make the request to get system metadata
|
|
|
|
const doLogin = (auth: IAuth) => {
|
|
|
|
const domain = auth.email.split("@")[1];
|
2024-08-27 14:00:30 -07:00
|
|
|
const well_known_url = "https://" + domain + "/.well-known/jmap";
|
|
|
|
const basic_auth =
|
|
|
|
"Basic " + base64.encode(auth.email + ":" + auth.password);
|
|
|
|
|
2024-08-27 14:11:00 -07:00
|
|
|
state.client = new Client({
|
2024-08-27 14:00:30 -07:00
|
|
|
accessToken: "fake token",
|
|
|
|
httpHeaders: { Authorization: basic_auth },
|
|
|
|
sessionUrl: well_known_url,
|
|
|
|
transport: new FetchTransport(fetch.bind(window)),
|
|
|
|
});
|
2024-08-27 14:11:00 -07:00
|
|
|
|
|
|
|
state.client
|
2024-08-27 14:00:30 -07:00
|
|
|
.fetchSession()
|
|
|
|
.then((response) => console.log(response))
|
|
|
|
.catch((error) => console.error(error));
|
|
|
|
|
|
|
|
return;
|
2024-08-27 10:18:24 -07:00
|
|
|
};
|
2024-08-27 09:39:28 -07:00
|
|
|
|
2024-08-27 11:11:54 -07:00
|
|
|
const loadAuth = () => {
|
|
|
|
const data = localStorage.getItem("auth");
|
|
|
|
if (!data) return;
|
|
|
|
const auth = JSON.parse(data);
|
2024-08-27 14:11:00 -07:00
|
|
|
state.auth = auth;
|
|
|
|
if (state.client == null) {
|
|
|
|
console.log("NULL STATE.client");
|
|
|
|
doLogin(state.auth);
|
|
|
|
return;
|
|
|
|
}
|
2024-08-27 09:39:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
loadAuth();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="App">
|
2024-08-27 14:00:30 -07:00
|
|
|
{state && state.auth ? (
|
|
|
|
<p>{state.auth.email}</p>
|
|
|
|
) : (
|
|
|
|
<AuthModal onLogin={onLogin}></AuthModal>
|
|
|
|
)}
|
2024-08-27 09:39:28 -07:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2024-07-31 20:58:46 -07:00
|
|
|
|
|
|
|
export default App;
|