/** * * AuthPage * */ import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { bindActionCreators, compose } from 'redux'; import { Link } from 'react-router-dom'; import { FormattedMessage } from 'react-intl'; import { findIndex, get, isBoolean, isEmpty, map, replace } from 'lodash'; import cn from 'classnames'; import pluginId from 'pluginId'; // Logo import LogoStrapi from 'assets/images/logo_strapi.png'; // Design import Button from 'components/Button'; import Input from 'components/InputsIndex'; // Utils import auth from 'utils/auth'; import { hideLoginErrorsInput, onChangeInput, setErrors, setForm, submit, } from './actions'; import form from './form.json'; import reducer from './reducer'; import saga from './saga'; import makeSelectAuthPage from './selectors'; import styles from './styles.scss'; export class AuthPage extends React.Component { // eslint-disable-line react/prefer-stateless-function componentDidMount() { auth.clearAppStorage(); this.setForm(); } componentDidUpdate(prevProps) { const { hideLoginErrorsInput, match: { params : { authType, }, }, submitSuccess, } = this.props; if (authType !== prevProps.match.params.authType) { this.setForm(); hideLoginErrorsInput(false); } if (submitSuccess) { switch (authType) { case 'login': case 'reset-password': // Check if we have token to handle redirection to login or admin. // Done to prevent redirection to admin after reset password if user should // not have access. auth.getToken() ? this.redirect('/') : this.redirect('/plugins/users-permissions/auth/login'); break; case 'register': this.redirect('/'); // NOTE: prepare for comfirm email; // this.redirect(`/plugins/users-permissions/auth/register-success/${this.props.modifiedData.email}`); break; default: } } } // Get form Errors shortcut. getFormErrors = () => { const { formErrors } = this.props; return get(formErrors, ['0', 'errors', '0', 'id']); } setForm = () => { const { location: { search, }, match: { params: { authType, id, }, }, setForm, } = this.props; const params = search ? replace(search, '?code=', '') : id; setForm(authType, params); } isAuthType = type => { const { match: { params: { authType } } } = this.props; return authType === type; } handleSubmit = (e) => { const { modifiedData, setErrors, submit } = this.props; e.preventDefault(); const formErrors = Object.keys(modifiedData).reduce((acc, key) => { if (isEmpty(get(modifiedData, key)) && !isBoolean(get(modifiedData, key))) { acc.push({ name: key, errors: [{ id: 'components.Input.error.validation.required' }] }); } if (!isEmpty(get(modifiedData, 'password')) && !isEmpty(get(modifiedData, 'confirmPassword')) && findIndex(acc, ['name', 'confirmPassword']) === -1) { if (modifiedData.password.length < 6) { acc.push({ name: 'password', errors: [{ id: 'users-permissions.components.Input.error.password.length' }] }); } if (get(modifiedData, 'password') !== get(modifiedData, 'confirmPassword')) { acc.push({ name: 'confirmPassword', errors: [{ id: 'users-permissions.components.Input.error.password.noMatch' }] }); } } return acc; }, []); setErrors(formErrors); if (isEmpty(formErrors)) { submit(this.context); } } redirect = path => this.props.history.push(path); renderButton = () => { const { match: { params: { authType } }, submitSuccess } = this.props; if (this.isAuthType('login')) { return (