import React from 'react'; import { defineMessages, useIntl } from 'react-intl'; import { useDispatch } from 'react-redux'; import { Link, Redirect } from 'react-router-dom'; import { logIn, verifyCredentials } from 'soapbox/actions/auth'; import { fetchInstance } from 'soapbox/actions/instance'; import { useAppSelector, useFeatures, useSoapboxConfig } from 'soapbox/hooks'; import { openModal } from '../../../actions/modals'; import { Button, Form, HStack, IconButton, Input, Tooltip } from '../../../components/ui'; import Pulse from './pulse'; import type { AxiosError } from 'axios'; const messages = defineMessages({ home: { id: 'header.home.label', defaultMessage: 'Home' }, login: { id: 'header.login.label', defaultMessage: 'Log in' }, register: { id: 'header.register.label', defaultMessage: 'Register' }, username: { id: 'header.login.username.placeholder', defaultMessage: 'Email or username' }, password: { id: 'header.login.password.label', defaultMessage: 'Password' }, forgotPassword: { id: 'header.login.forgot_password', defaultMessage: 'Forgot password?' }, }); const Header = () => { const dispatch = useDispatch(); const intl = useIntl(); const { logo } = useSoapboxConfig(); const features = useFeatures(); const instance = useAppSelector((state) => state.instance); const isOpen = instance.get('registrations', false) === true; const pepeOpen = useAppSelector(state => state.verification.getIn(['instance', 'registrations'], false) === true); const [isLoading, setLoading] = React.useState(false); const [username, setUsername] = React.useState(''); const [password, setPassword] = React.useState(''); const [shouldRedirect, setShouldRedirect] = React.useState(false); const [mfaToken, setMfaToken] = React.useState(false); const open = () => dispatch(openModal('LANDING_PAGE')); const handleSubmit: React.FormEventHandler = (event) => { event.preventDefault(); setLoading(true); dispatch(logIn(intl, username, password) as any) .then(({ access_token }: { access_token: string }) => { return ( dispatch(verifyCredentials(access_token) as any) // Refetch the instance for authenticated fetch .then(() => dispatch(fetchInstance())) .then(() => setShouldRedirect(true)) ); }) .catch((error: AxiosError) => { setLoading(false); const data = error.response?.data; if (data?.error === 'mfa_required') { setMfaToken(data.mfa_token); } }); }; if (shouldRedirect) return ; if (mfaToken) return ; return (
); }; export default Header;