diff --git a/packages/component-authentication/src/components/LoginPage.js b/packages/component-authentication/src/components/LoginPage.js index 2f78b386d00ece5177096ad6a4f7a9833a637d24..21ec2c0767493c0079f790574037fad84e96de3c 100644 --- a/packages/component-authentication/src/components/LoginPage.js +++ b/packages/component-authentication/src/components/LoginPage.js @@ -1,18 +1,9 @@ -import { get } from 'lodash' -import config from 'config' import { reduxForm, SubmissionError } from 'redux-form' import { compose } from 'recompose' import { connect } from 'react-redux' import { login } from '../redux/login' import Login from './Login' - -const redirectPath = ({ location: { state } }) => { - const redirect = get(config, ['pubsweet-client', 'login-redirect'], '/') - - return state && state.from && state.from.pathname !== '/logout' - ? state.from.pathname - : redirect -} +import redirectPath from '../redirect' const onSubmit = (values, dispatch, { history, location }) => { dispatch(login(values)) diff --git a/packages/component-authentication/src/components/SignupPage.js b/packages/component-authentication/src/components/SignupPage.js index 0e8a0fffd625ffdb1b019ccfaeedfacad2eb92fd..74443dcb6a4d04ee32f69eb8696cfb79cc3ff77a 100644 --- a/packages/component-authentication/src/components/SignupPage.js +++ b/packages/component-authentication/src/components/SignupPage.js @@ -3,16 +3,21 @@ import { compose } from 'recompose' import { reduxForm, SubmissionError } from 'redux-form' import { signup } from '../redux/signup' import Signup from './Signup' +import redirectPath from '../redirect' -const onSubmit = (values, dispatch) => { - dispatch(signup(values)).catch(error => { - if (error.validationErrors) { - throw new SubmissionError(error.validationErrors) - } else { - console.error(error) - // TODO: display error - } - }) +const onSubmit = (values, dispatch, { history, location }) => { + dispatch(signup(values)) + .then(() => { + history.push(redirectPath({ location })) + }) + .catch(error => { + if (error.validationErrors) { + throw new SubmissionError(error.validationErrors) + } else { + console.error(error) + // TODO: display error + } + }) } export default compose( diff --git a/packages/component-authentication/src/redirect.js b/packages/component-authentication/src/redirect.js new file mode 100644 index 0000000000000000000000000000000000000000..fad202d048b96a1692c4c069044c7022c1b20164 --- /dev/null +++ b/packages/component-authentication/src/redirect.js @@ -0,0 +1,10 @@ +import { get } from 'lodash' +import config from 'config' + +const allowedRedirect = pathname => + ['/logout', '/login', '/signup'].indexOf(pathname) === -1 + +export default ({ location: { state } }) => + state && state.from && allowedRedirect(state.from.pathname) + ? state.from.pathname + : get(config, ['pubsweet-client', 'login-redirect'], '/')