import React from 'react';
import { defineMessages, FormattedMessage, 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 { openModal } from 'soapbox/actions/modals';
import SiteLogo from 'soapbox/components/site-logo';
import { Button, Form, HStack, IconButton, Input, Tooltip } from 'soapbox/components/ui';
import { useAppSelector, useFeatures, useSoapboxConfig, useOwnAccount } from 'soapbox/hooks';
import Sonar from './sonar';
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 account = useOwnAccount();
const soapboxConfig = useSoapboxConfig();
const pepeEnabled = soapboxConfig.getIn(['extensions', 'pepe', 'enabled']) === true;
const { links } = soapboxConfig;
const features = useFeatures();
const instance = useAppSelector((state) => state.instance);
const isOpen = features.accountCreation && instance.registrations;
const pepeOpen = useAppSelector(state => state.verification.instance.get('registrations') === 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(username, password) as any)
.then(({ access_token }: { access_token: string }) => (
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: any = error.response?.data;
if (data?.error === 'mfa_required') {
setMfaToken(data.mfa_token);
}
});
};
if (account && shouldRedirect) return ;
if (mfaToken) return ;
return (
);
};
export default Header;