diff --git a/src/App.tsx b/src/App.tsx index 2b25796..6f70c48 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,18 +7,28 @@ import AuthModal from "./AuthModal"; import React, { useEffect, useState } from "react"; interface IAuth { + email: string; password: string; - username: string; } const App = () => { const [state, setInternalState] = useState(null); - const doLogin = (email: string, password: string) => { - const domain = email.split("@")[1]; + // When the user provides credentials + const onLogin = (email: string, password: string) => { + // Store the provided credentials for now + const auth = {email: email, password: password} + setInternalState(auth); + localStorage.setItem("auth", JSON.stringify(auth)); + doLogin(auth); + }; + + // Make the request to get system metadata + const doLogin = (auth: IAuth) => { + const domain = auth.email.split("@")[1]; const well_known_url = "https://" + domain + "/.well-known/jmap" let headers = new Headers(); - headers.append("Authorization", "Basic " + base64.encode(email + ":" + password)); + headers.append("Authorization", "Basic " + base64.encode(auth.email + ":" + auth.password)); fetch(well_known_url, { method: "GET", headers: headers, @@ -27,24 +37,22 @@ const App = () => { .then(data => console.log(data)) .catch(error => console.error(error)); }; - const loadAuth = () => { - const auth = localStorage.getItem("auth"); - if (!auth) return; - setInternalState(JSON.parse(auth)); - }; - const setState = (auth: IAuth) => { - localStorage.setItem("auth", JSON.stringify(auth)); + const loadAuth = () => { + const data = localStorage.getItem("auth"); + if (!data) return; + const auth = JSON.parse(data); + setInternalState(auth); + doLogin(auth); }; useEffect(() => { loadAuth(); - //fetchUserData() }, []); return (
- {state ?

{state.username}

: } + {state ?

{state.email}

: }
); }; diff --git a/src/AuthModal.tsx b/src/AuthModal.tsx index dd5b1fa..999cea3 100644 --- a/src/AuthModal.tsx +++ b/src/AuthModal.tsx @@ -5,10 +5,10 @@ import Modal from "react-bootstrap/Modal" import React, {useState} from "react" type AuthProps = { - doLogin: (email: string, password: string) => void; + onLogin: (email: string, password: string) => void; } -const AuthModal: React.FC = ({ doLogin }) => { +const AuthModal: React.FC = ({ onLogin }) => { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); @@ -23,7 +23,7 @@ const AuthModal: React.FC = ({ doLogin }) => { -
{e.preventDefault(); doLogin(email, password)}}> + {e.preventDefault(); onLogin(email, password)}}> Email address setEmail(e.target.value)}/>