gothinkster/react-redux-realworld-example-app

Auth redirection not working

dhaval-cygnet opened this issue · 1 comments

When I open the article editor page when I am not logged in then app not redirecting to login page.

create private route file as follows:

import React from 'react';
import {Route, Redirect} from 'react-router-dom';

const PrivateRoute = ({ component: Comp, loggedIn, path, ...rest }) => {
  return (
    <Route
      {...rest}
      render={props => {
        return loggedIn ? (
          <Comp {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/",
              state: {
                prevLocation: path,
                error: "You need to login first!",
              },
            }}
          />
        );
      }}
    />
  );
};

export default PrivateRoute;

and then in your App.js use it like follows

<PrivateRoute path="/protected-page" loggedIn={this.props.currentUser ? true : false} component={ProtectedComponent} />