Fix location state handling for proper redirects

This commit is contained in:
Kimmo Puputti 2017-02-14 20:35:53 +02:00
parent 022c175c2d
commit 862f01f00b
3 changed files with 16 additions and 10 deletions

View file

@ -7,12 +7,13 @@ import { NamedRedirect } from './components';
const Routes = props => {
const { isAuthenticated, routes } = props;
const renderComponent = (route, match) => {
const renderComponent = (route, matchProps) => {
const { auth, component: Component } = route;
const { match, location } = matchProps;
const canShowComponent = !auth || isAuthenticated;
return canShowComponent
? <Component {...match} />
: <NamedRedirect name="LogInPage" state={{ from: match.location }} />;
? <Component params={match.params} location={location} />
: <NamedRedirect name="LogInPage" state={{ from: match.url }} />;
};
const toRouteComponent = route => (
@ -20,7 +21,7 @@ const Routes = props => {
key={route.name}
path={route.pattern}
exact={route.exactly}
render={matchProps => renderComponent(route, matchProps.match)}
render={matchProps => renderComponent(route, matchProps)}
/>
);

View file

@ -9,15 +9,14 @@ export const AuthenticationPageComponent = props => {
const { location, tab, isAuthenticated, onLoginSubmit, onSignUpSubmit } = props;
const isLogin = tab === 'login';
const from = location.state && location.state.from ? location.state.from : null;
const pathname = from ? from.pathname : null;
return (
<PageLayout title={`Authentication page: ${tab} tab`}>
{isAuthenticated ? <Redirect to={from || '/'} /> : null}
{pathname
{from
? <p>
You must log in to view the page at
<code>{pathname}</code>
<code>{from}</code>
</p>
: null}
{isLogin ? <LoginForm onSubmit={onLoginSubmit} /> : <SignUpForm onSubmit={onSignUpSubmit} />}
@ -28,12 +27,12 @@ export const AuthenticationPageComponent = props => {
);
};
AuthenticationPageComponent.defaultProps = { location: {}, tab: 'signup' };
AuthenticationPageComponent.defaultProps = { tab: 'signup' };
const { any, oneOf, shape, bool, func } = PropTypes;
const { object, oneOf, shape, bool, func } = PropTypes;
AuthenticationPageComponent.propTypes = {
location: shape({ state: shape({ from: any }) }),
location: shape({ state: object }).isRequired,
tab: oneOf(['login', 'signup']),
isAuthenticated: bool.isRequired,
onLoginSubmit: func.isRequired,

View file

@ -1,6 +1,12 @@
exports[`AuthenticationPageComponent matches snapshot 1`] = `
<Connect(PageLayout)
title="Authentication page: signup tab">
<p>
You must log in to view the page at
<code>
/protected
</code>
</p>
<ReduxForm
onSubmit={[Function]} />
<Link