import localforage from "localforage"; import axios from 'axios'; import { FunctionComponent, useCallback, useState } from "react"; import { Button } from '@revoltchat/ui/lib/components/atoms/inputs/Button'; import { InputBox } from '@revoltchat/ui/lib/components/atoms/inputs/InputBox'; import { H1 } from '@revoltchat/ui/lib/components/atoms/heading/H1'; import { H2 } from '@revoltchat/ui/lib/components/atoms/heading/H2'; import { API_URL } from "../App"; const Login: FunctionComponent = () => { const [username, setUsername] = useState(''); const [showInitial, setShowInitial] = useState(true); const [showSecond, setShowSecond] = useState(false); const [statusMsg, setStatusMsg] = useState(''); const [code, setCode] = useState(''); const [nonce, setNonce] = useState(''); const getCode = useCallback(async () => { if (!username) return; setShowInitial(false); try { const res = await axios.post(`${API_URL}/login/begin`, { user: username }); setShowSecond(true); setCode(res.data.code); setNonce(res.data.nonce); setUsername(res.data.uid); } catch(e: any) { setStatusMsg(e?.message || e); setShowInitial(true); setShowSecond(false); } }, [ username ]); const getSession = useCallback(async () => { try { const res = await axios.post(`${API_URL}/login/complete`, { nonce, code, user: username }); await localforage.setItem('auth', { user: res.data.user, token: res.data.token }); setShowSecond(false); window.location.reload(); } catch(e: any) { setStatusMsg(e?.message || e); } }, [ nonce, code, username ]); return (

log in

{statusMsg.length ? {statusMsg} :
}
); } export default Login;