import React, { useState } from 'react'; import PropTypes from 'prop-types'; import './AuthRequest.scss'; import initMatrix from '../../../client/initMatrix'; import { openReusableDialog } from '../../../client/action/navigation'; import Text from '../../atoms/text/Text'; import Button from '../../atoms/button/Button'; import Input from '../../atoms/input/Input'; import Spinner from '../../atoms/spinner/Spinner'; import { useStore } from '../../hooks/useStore'; let lastUsedPassword; const getAuthId = (password) => ({ type: 'm.login.password', password, identifier: { type: 'm.id.user', user: initMatrix.matrixClient.getUserId(), }, }); function AuthRequest({ onComplete, makeRequest }) { const [status, setStatus] = useState(false); const mountStore = useStore(); const handleForm = async (e) => { mountStore.setItem(true); e.preventDefault(); const password = e.target.password.value; if (password.trim() === '') return; try { setStatus({ ongoing: true }); await makeRequest(getAuthId(password)); lastUsedPassword = password; if (!mountStore.getItem()) return; onComplete(true); } catch (err) { lastUsedPassword = undefined; if (!mountStore.getItem()) return; if (err.errcode === 'M_FORBIDDEN') { setStatus({ error: 'Wrong password. Please enter correct password.' }); return; } setStatus({ error: 'Request failed!' }); } }; const handleChange = () => { setStatus(false); }; return (