import classNames from 'clsx'; import React, { useRef, useState } 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 { openSidebar } from 'soapbox/actions/sidebar'; import SiteLogo from 'soapbox/components/site-logo'; import { Avatar, Button, Form, IconButton, Input, Tooltip } from 'soapbox/components/ui'; import Search from 'soapbox/features/compose/components/search'; import { useOwnAccount, useSoapboxConfig } from 'soapbox/hooks'; import ProfileDropdown from './profile-dropdown'; import type { AxiosError } from 'axios'; const messages = defineMessages({ login: { id: 'navbar.login.action', defaultMessage: 'Log in' }, username: { id: 'navbar.login.username.placeholder', defaultMessage: 'Email or username' }, password: { id: 'navbar.login.password.label', defaultMessage: 'Password' }, forgotPassword: { id: 'navbar.login.forgot_password', defaultMessage: 'Forgot password?' }, }); const Navbar = () => { const dispatch = useDispatch(); const intl = useIntl(); const node = useRef(null); const account = useOwnAccount(); const soapboxConfig = useSoapboxConfig(); const singleUserMode = soapboxConfig.get('singleUserMode'); const [isLoading, setLoading] = useState(false); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [mfaToken, setMfaToken] = useState(false); const onOpenSidebar = () => dispatch(openSidebar()); const handleSubmit: React.FormEventHandler = (event) => { event.preventDefault(); setLoading(true); dispatch(logIn(username, password) as any) .then(({ access_token }: { access_token: string }) => { setLoading(false); return ( dispatch(verifyCredentials(access_token) as any) // Refetch the instance for authenticated fetch .then(() => dispatch(fetchInstance())) ); }) .catch((error: AxiosError) => { setLoading(false); const data: any = error.response?.data; if (data?.error === 'mfa_required') { setMfaToken(data.mfa_token); } }); }; if (mfaToken) return ; return ( ); }; export default Navbar;