/** * Login route * * @author Pierre Hubert */ import { Avatar, Box, Button, Container, CssBaseline, List, ListItem, ListItemAvatar, ListItemText, Paper, TextField, Typography, } from "@material-ui/core"; import { ErrorOutline, Lock, VpnKey } from "@material-ui/icons"; import LockOutlinedIcon from "@material-ui/icons/LockOutlined"; import React from "react"; import { AccountHelper, AuthOptions } from "../../helpers/AccountHelper"; import { input, matAlert } from "../widgets/DialogsProvider"; function ErrorGettingOptions() { return (   Could not get your auth options! ); } function Copyright() { return ( {"Copyright © "} Comunic  {new Date().getFullYear()} {"."} ); } interface LoginRouteState { currEmail: string; errorGettingAuthOptions: boolean; authOptions?: AuthOptions; } export class LoginRoute extends React.Component<{}, LoginRouteState> { constructor(props: any) { super(props); this.state = { currEmail: "", errorGettingAuthOptions: false, authOptions: undefined, }; this.handleChangedEmail = this.handleChangedEmail.bind(this); this.handleSubmitEmail = this.handleSubmitEmail.bind(this); } get canSubmit(): boolean { return this.state.currEmail.length > 4; } handleChangedEmail(e: React.ChangeEvent) { this.setState({ currEmail: e.target.value, errorGettingAuthOptions: false, authOptions: undefined, }); } async handleSubmitEmail(e: React.ChangeEvent) { try { e.preventDefault(); if (!this.canSubmit) return; const options = await AccountHelper.getAuthOptions( this.state.currEmail ); this.setState({ errorGettingAuthOptions: false, authOptions: options, }); } catch (e) { console.error("Failed to get auth options!", e); this.setState({ errorGettingAuthOptions: true }); } } render() { return (
Comunic Console
{/* Login error (if any) */} {this.state.errorGettingAuthOptions ? ( ErrorGettingOptions() ) : (
)} {/* Auth options */} {this.state.authOptions ? ( ) : (
)}
); } } interface AuthOptionsWidgetProps { email: string; options: AuthOptions; } interface AuthOptionsWidgetState {} class AuthOptionsWidget extends React.Component< AuthOptionsWidgetProps, AuthOptionsWidgetState > { constructor(props: Readonly) { super(props); this.state = {}; this.loginWithResetToken = this.loginWithResetToken.bind(this); this.loginWithSecurityKey = this.loginWithSecurityKey.bind(this); } async loginWithResetToken() { try { const token = await input({ label: "Reset token", message: "Please specify here your token:", minLength: 2, title: "Login using access token", }); await AccountHelper.authWithResetToken(this.props.email, token); document.location.href = document.location.href + ""; } catch (e) { console.error(e); matAlert("Authentication failed!"); } } async loginWithSecurityKey(id: number) { console.info(id); } render() { return ( {/* Password reset token */} {this.props.options.reset_token ? ( ) : ( )} {this.props.options.keys.map((key) => ( this.loginWithSecurityKey(key.id)} key={key.id} > ))} ); } }