2017-11-09 17:36:07 +01:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* AuthPage
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2017-11-10 14:20:33 +01:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2017-11-10 12:26:51 +01:00
|
|
|
import { findIndex, get, isBoolean, isEmpty, map } from 'lodash';
|
2017-11-09 18:07:55 +01:00
|
|
|
import cn from 'classnames';
|
2017-11-10 14:20:33 +01:00
|
|
|
|
|
|
|
|
// Logo
|
|
|
|
|
import LogoStrapi from 'assets/images/logo_strapi.png';
|
|
|
|
|
|
2017-11-09 17:36:07 +01:00
|
|
|
// Design
|
2017-11-09 18:07:55 +01:00
|
|
|
import Button from 'components/Button';
|
2017-11-09 17:36:07 +01:00
|
|
|
import Input from 'components/Input';
|
|
|
|
|
|
|
|
|
|
// Utils
|
|
|
|
|
import injectSaga from 'utils/injectSaga';
|
|
|
|
|
import injectReducer from 'utils/injectReducer';
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
onChangeInput,
|
2017-11-09 18:07:55 +01:00
|
|
|
setErrors,
|
2017-11-09 17:36:07 +01:00
|
|
|
setForm,
|
2017-11-10 14:20:33 +01:00
|
|
|
submit,
|
2017-11-09 17:36:07 +01:00
|
|
|
} 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() {
|
|
|
|
|
this.props.setForm(this.props.match.params.authType, this.props.match.params.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
|
if (this.props.match.params.authType !== nextProps.match.params.authType) {
|
2017-11-10 14:20:33 +01:00
|
|
|
this.props.setForm(nextProps.match.params.authType, nextProps.match.params.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (nextProps.submitSuccess) {
|
|
|
|
|
switch (this.props.match.params.authType) {
|
|
|
|
|
case 'login':
|
|
|
|
|
this.props.history.push('/');
|
|
|
|
|
break;
|
|
|
|
|
case 'register':
|
2017-11-16 15:51:12 +01:00
|
|
|
this.props.history.push('/');
|
|
|
|
|
// NOTE: prepare for comfirm email;
|
|
|
|
|
// this.props.history.push(`/plugins/users-permissions/auth/register-success/${this.props.modifiedData.email}`);
|
2017-11-10 14:20:33 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
|
|
}
|
2017-11-09 17:36:07 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 18:07:55 +01:00
|
|
|
handleSubmit = (e) => {
|
|
|
|
|
e.preventDefault();
|
2017-11-10 12:26:51 +01:00
|
|
|
const formErrors = Object.keys(this.props.modifiedData).reduce((acc, key) => {
|
|
|
|
|
if (isEmpty(get(this.props.modifiedData, key)) && !isBoolean(get(this.props.modifiedData, key))) {
|
|
|
|
|
acc.push({ name: key, errors: [{ id: 'components.Input.error.validation.required' }] });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!isEmpty(get(this.props.modifiedData, 'password')) && !isEmpty(get(this.props.modifiedData, 'confirmPassword')) && findIndex(acc, ['name', 'confirmPassword']) === -1) {
|
|
|
|
|
if (get(this.props.modifiedData, 'password') !== get(this.props.modifiedData, 'confirmPassword')) {
|
|
|
|
|
acc.push({ name: 'confirmPassword', errors: [{ id: 'users-permissions.components.Input.error.password.noMatch' }] });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
this.props.setErrors(formErrors);
|
2017-11-10 14:20:33 +01:00
|
|
|
|
|
|
|
|
if (isEmpty(formErrors)) {
|
|
|
|
|
this.props.submit();
|
|
|
|
|
}
|
2017-11-09 18:07:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
renderButton = () => {
|
|
|
|
|
if (this.props.match.params.authType === 'login') {
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn('col-md-6', styles.loginButton)}>
|
|
|
|
|
<Button primary label="users-permissions.Auth.form.button.login" type="submit" />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn('col-md-12', styles.buttonContainer)}>
|
|
|
|
|
<Button
|
|
|
|
|
label={`users-permissions.Auth.form.button.${this.props.match.params.authType}`}
|
|
|
|
|
style={{ width: '100%' }}
|
2017-11-10 14:20:33 +01:00
|
|
|
primary
|
2017-11-09 18:07:55 +01:00
|
|
|
type="submit"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 14:20:33 +01:00
|
|
|
renderLink = () => {
|
|
|
|
|
|
|
|
|
|
if (this.props.match.params.authType === 'login') {
|
|
|
|
|
return (
|
|
|
|
|
<Link to="/plugins/users-permissions/auth/forgot-password">
|
|
|
|
|
<FormattedMessage id="users-permissions.Auth.link.forgot-password" />
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.props.match.params.authType === 'forgot-password' || this.props.match.params.authType === 'register-success') {
|
|
|
|
|
return (
|
|
|
|
|
<Link to="/plugins/users-permissions/auth/login">
|
|
|
|
|
<FormattedMessage id="users-permissions.Auth.link.ready" />
|
|
|
|
|
</Link>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <div />;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-09 17:36:07 +01:00
|
|
|
render() {
|
|
|
|
|
const inputs = get(form, ['form', this.props.match.params.authType]);
|
2017-11-14 10:31:18 +01:00
|
|
|
const divStyle = this.props.match.params.authType === 'register' ? { marginTop: '3.2rem' } : { marginTop: '.9rem' };
|
2017-11-10 14:20:33 +01:00
|
|
|
const headerDescription = this.props.match.params.authType === 'register' ?
|
|
|
|
|
<FormattedMessage id="users-permissions.Auth.header.register.description" />
|
|
|
|
|
: <span />;
|
|
|
|
|
|
2017-11-09 17:36:07 +01:00
|
|
|
return (
|
|
|
|
|
<div className={styles.authPage}>
|
|
|
|
|
<div className={styles.wrapper}>
|
2017-11-10 14:20:33 +01:00
|
|
|
<div className={styles.headerContainer}>
|
|
|
|
|
{this.props.match.params.authType === 'register' ? (
|
|
|
|
|
<FormattedMessage id="users-permissions.Auth.form.header.register" />
|
|
|
|
|
) : (
|
|
|
|
|
<img src={LogoStrapi} alt="logo" />
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className={styles.headerDescription}>
|
|
|
|
|
{headerDescription}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className={styles.formContainer} style={divStyle}>
|
2017-11-09 18:07:55 +01:00
|
|
|
<form onSubmit={this.handleSubmit}>
|
|
|
|
|
<div className="container-fluid">
|
|
|
|
|
<div className="row" style={{ textAlign: 'start' }}>
|
2017-11-09 18:09:48 +01:00
|
|
|
{map(inputs, (input, key) => (
|
|
|
|
|
<Input
|
|
|
|
|
autoFocus={key === 0}
|
|
|
|
|
customBootstrapClass={get(input, 'customBootstrapClass')}
|
2017-11-10 12:26:51 +01:00
|
|
|
didCheckErrors={this.props.didCheckErrors}
|
|
|
|
|
errors={get(this.props.formErrors, [findIndex(this.props.formErrors, ['name', input.name]), 'errors'])}
|
2017-11-09 18:09:48 +01:00
|
|
|
key={get(input, 'name')}
|
|
|
|
|
label={get(input, 'label')}
|
|
|
|
|
name={get(input, 'name')}
|
|
|
|
|
onChange={this.props.onChangeInput}
|
|
|
|
|
placeholder={get(input, 'placeholder')}
|
|
|
|
|
type={get(input, 'type')}
|
|
|
|
|
validations={{ required: true }}
|
|
|
|
|
value={get(this.props.modifiedData, get(input, 'name'))}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
{this.renderButton()}
|
2017-11-09 18:07:55 +01:00
|
|
|
</div>
|
2017-11-09 17:36:07 +01:00
|
|
|
</div>
|
2017-11-09 18:07:55 +01:00
|
|
|
</form>
|
2017-11-09 17:36:07 +01:00
|
|
|
</div>
|
2017-11-10 14:20:33 +01:00
|
|
|
<div className={styles.linkContainer}>
|
|
|
|
|
{this.renderLink()}
|
|
|
|
|
</div>
|
2017-11-09 17:36:07 +01:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AuthPage.propTypes = {
|
2017-11-10 12:26:51 +01:00
|
|
|
didCheckErrors: PropTypes.bool.isRequired,
|
|
|
|
|
formErrors: PropTypes.array.isRequired,
|
2017-11-10 14:20:33 +01:00
|
|
|
history: PropTypes.object.isRequired,
|
2017-11-09 17:36:07 +01:00
|
|
|
match: PropTypes.object.isRequired,
|
|
|
|
|
modifiedData: PropTypes.object.isRequired,
|
|
|
|
|
onChangeInput: PropTypes.func.isRequired,
|
2017-11-09 18:07:55 +01:00
|
|
|
setErrors: PropTypes.func.isRequired,
|
2017-11-09 17:36:07 +01:00
|
|
|
setForm: PropTypes.func.isRequired,
|
2017-11-10 14:20:33 +01:00
|
|
|
submit: PropTypes.func.isRequired,
|
|
|
|
|
submitSuccess: PropTypes.bool.isRequired,
|
2017-11-09 17:36:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const mapStateToProps = makeSelectAuthPage();
|
|
|
|
|
|
|
|
|
|
function mapDispatchToProps(dispatch) {
|
|
|
|
|
return bindActionCreators(
|
|
|
|
|
{
|
|
|
|
|
onChangeInput,
|
2017-11-09 18:07:55 +01:00
|
|
|
setErrors,
|
2017-11-09 17:36:07 +01:00
|
|
|
setForm,
|
2017-11-10 14:20:33 +01:00
|
|
|
submit,
|
2017-11-09 17:36:07 +01:00
|
|
|
},
|
|
|
|
|
dispatch
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const withConnect = connect(mapStateToProps, mapDispatchToProps);
|
|
|
|
|
|
|
|
|
|
/* Remove this line if the container doesn't have a route and
|
|
|
|
|
* check the documentation to see how to create the container's store
|
|
|
|
|
*/
|
|
|
|
|
const withReducer = injectReducer({ key: 'authPage', reducer });
|
|
|
|
|
|
|
|
|
|
/* Remove the line below the container doesn't have a route and
|
|
|
|
|
* check the documentation to see how to create the container's store
|
|
|
|
|
*/
|
|
|
|
|
const withSaga = injectSaga({ key: 'authPage', saga });
|
|
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
|
withReducer,
|
|
|
|
|
withSaga,
|
|
|
|
|
withConnect,
|
|
|
|
|
)(AuthPage);
|