import React from 'react'; import { connect } from 'react-redux'; import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePureComponent from 'react-immutable-pure-component'; import { Link } from 'react-router-dom'; import { defineMessages, injectIntl } from 'react-intl'; import PropTypes from 'prop-types'; import SoapboxPropTypes from 'soapbox/utils/soapbox_prop_types'; import LoginForm from 'soapbox/features/auth_login/components/login_form'; import { logIn, verifyCredentials } from 'soapbox/actions/auth'; import { fetchInstance } from 'soapbox/actions/instance'; import OtpAuthForm from 'soapbox/features/auth_login/components/otp_auth_form'; import IconButton from 'soapbox/components/icon_button'; import SiteLogo from './site_logo'; const messages = defineMessages({ home: { id: 'header.home.label', defaultMessage: 'Home' }, about: { id: 'header.about.label', defaultMessage: 'About' }, backTo: { id: 'header.back_to.label', defaultMessage: 'Back to {siteTitle}' }, login: { id: 'header.login.label', defaultMessage: 'Log in' }, close: { id: 'lightbox.close', defaultMessage: 'Close' }, }); const mapStateToProps = state => ({ me: state.get('me'), instance: state.get('instance'), isLoading: false, }); export default @connect(mapStateToProps) @injectIntl class Header extends ImmutablePureComponent { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); } state = { isLoading: false, mfa_auth_needed: false, mfa_token: '', } getFormData = (form) => { return Object.fromEntries( Array.from(form).map(i => [i.name, i.value]), ); } static contextTypes = { router: PropTypes.object, }; handleSubmit = (event) => { const { dispatch, intl } = this.props; const { username, password } = this.getFormData(event.target); dispatch(logIn(intl, username, password)).then(({ access_token }) => { return dispatch(verifyCredentials(access_token)) // Refetch the instance for authenticated fetch .then(() => dispatch(fetchInstance())); }).catch(error => { if (error.response.data.error === 'mfa_required') { this.setState({ mfa_auth_needed: true, mfa_token: error.response.data.mfa_token }); } this.setState({ isLoading: false }); }); this.setState({ isLoading: true }); event.preventDefault(); } onClickClose = (event) => { this.setState({ mfa_auth_needed: false, mfa_token: '' }); } static propTypes = { me: SoapboxPropTypes.me, instance: ImmutablePropTypes.map, intl: PropTypes.object.isRequired, } render() { const { me, instance, intl } = this.props; const { isLoading, mfa_auth_needed, mfa_token } = this.state; return ( ); } }