diff --git a/src/EteSyncContext.tsx b/src/EteSyncContext.tsx index 47770bd..1309ab5 100644 --- a/src/EteSyncContext.tsx +++ b/src/EteSyncContext.tsx @@ -9,9 +9,14 @@ import * as EteSync from './api/EteSync'; import { routeResolver } from './App'; const SERVICE_API = 'http://localhost:8000'; -const USER = 'me@etesync.com'; -const PASSWORD = ''; -const derived = EteSync.deriveKey(USER, PASSWORD); + +const CONTEXT_SESSION_KEY = 'EteSyncContext'; + +enum LoadState { + Initial = 'INIT', + Working = 'WORKING', + Done = 'DONE', +} export interface EteSyncContextType { serviceApiUrl: string; @@ -20,13 +25,47 @@ export interface EteSyncContextType { } export class EteSyncContext extends React.Component { - static state: EteSyncContextType; + username: HTMLInputElement; + password: HTMLInputElement; + encryptionPassword: HTMLInputElement; + + state: { + context?: EteSyncContextType; + loadState: LoadState; + error?: Error; + }; + + constructor(props: any) { + super(props); + this.state = {loadState: LoadState.Initial}; + this.generateEncryption = this.generateEncryption.bind(this); - componentDidMount() { + const contextStr = sessionStorage.getItem(CONTEXT_SESSION_KEY); + + if (contextStr !== null) { + const context: EteSyncContextType = JSON.parse(contextStr); + + this.state = { + loadState: LoadState.Done, + context + }; + } + } + + generateEncryption() { let authenticator = new EteSync.Authenticator(SERVICE_API); - authenticator.getAuthToken(USER, PASSWORD).then((authToken) => { - const credentials = new EteSync.Credentials(USER, authToken); + this.setState({ + loadState: LoadState.Working + }); + + const username = this.username.value; + const password = this.password.value; + const encryptionPassword = this.encryptionPassword.value; + + authenticator.getAuthToken(username, password).then((authToken) => { + const credentials = new EteSync.Credentials(username, authToken); + const derived = EteSync.deriveKey(username, encryptionPassword); const context = { serviceApiUrl: SERVICE_API, @@ -34,15 +73,44 @@ export class EteSyncContext extends React.Component { encryptionKey: derived, }; - this.setState(context); + sessionStorage.setItem(CONTEXT_SESSION_KEY, JSON.stringify(context)); + + this.setState({ + loadState: LoadState.Done, + context + }); + }).catch((error) => { + this.setState({ + loadState: LoadState.Initial, + error + }); }); } render() { - if (this.state === null) { + if (this.state.loadState === LoadState.Initial) { + return ( +