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 LoginForm from 'soapbox/features/auth_login/components/login_form'; import SiteLogo from './site_logo'; import SoapboxPropTypes from 'soapbox/utils/soapbox_prop_types'; import { defineMessages, injectIntl } from 'react-intl'; import PropTypes from 'prop-types'; import { logIn } from 'soapbox/actions/auth'; import { fetchMe } from 'soapbox/actions/me'; import OtpAuthForm from 'soapbox/features/auth_login/components/otp_auth_form'; import IconButton from 'soapbox/components/icon_button'; 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' }, backToEnding: { id: 'header.back_to_ending.label', defaultMessage: ' ' }, 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); } getFormData = (form) => { return Object.fromEntries( Array.from(form).map(i => [i.name, i.value]) ); } static contextTypes = { router: PropTypes.object, }; handleSubmit = (event) => { const { dispatch } = this.props; const { username, password } = this.getFormData(event.target); dispatch(logIn(username, password)).then(() => { return dispatch(fetchMe()); }).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, } state = { mfa_auth_needed: false, mfa_token: '', } render() { const { me, instance, isLoading, intl } = this.props; const { mfa_auth_needed, mfa_token } = this.state; return ( ); } }