Skip to content
This repository has been archived by the owner on Sep 8, 2021. It is now read-only.

Auth redirection not working #109

Open
dhaval-cygnet opened this issue Jun 7, 2018 · 1 comment
Open

Auth redirection not working #109

dhaval-cygnet opened this issue Jun 7, 2018 · 1 comment

Comments

@dhaval-cygnet
Copy link

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

@khanof89
Copy link

khanof89 commented Dec 3, 2019

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} />

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants