0

There is one need for url authentication:

import React from "react";
import { connect } from "react-redux";
import { Switch, Route, Redirect } from "react-router-dom";
...

const IndexRouter = ({ loggedIn }) => (
  <Switch>
    <Route
      path="/"
      render={() => (loggedIn ? <Redirect to="/dashboard" /> : <Login />)}
    />
    <Route exact path="/dashboard" component={DashboardRouter} />
    <Route exact path="/stock" component={StockRouter} />
  </Switch>
);

export default connect(
  state => ({
    loggedIn: state.persist.loggedIn
  }),
  {}
)(IndexRouter);

The code means if I have not logged in, all of url are required from client will redirect to Login component. Other than that it will route to DashboardRouter. The StockRouter is another route related with DashboardRouter.

The problem is that if I logged in. All the unspecific url (except /dashboard, /stock) I manually typed showing the /dashboard url without anything. The specific url such as /stock can show the component StockRouter directly.

ccd
  • 5,788
  • 10
  • 46
  • 96

2 Answers2

0

You would need to write a PrivateRoute wrapper around your Route and change the order of Routes in IndexRouter, so that the Route with path / is matched at the last otherwise all routes will match / first and will not render correctly

const PrivateRoute = ({component: Component, loggedIn, ...rest }) => { 
           if(!loggedIn) {
               return <Redirect to="/login" />
           }
           return <Route {...rest} component={Component}/> 
        }
    } 
} 

const IndexRouter = ({ loggedIn }) => (
  <Switch>
    <PrivateRoute exact path="/dashboard" component={DashboardRouter} />
    <PrivateRoute exact path="/stock" component={StockRouter} />
    <Redirect to="/dashboard" />
  </Switch>
);

For more details, check Performing Authentication on Routes with react-router-v4

Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
  • There is a little bug in this solution, if the url redirect to `/dashboard`, the `` can not be rendered. – ccd Jul 11 '18 at 02:35
  • Replace `` to `` can ugly drop this problem, however it make me a few confusion now. – ccd Jul 11 '18 at 03:13
0

Just create a history component like this :

import React from "react";
import {withRouter} from "react-router";

let globalHistory = null;

class HistoryComponent extends React.Component {
  componentWillMount() {
    const {history} = this.props;
    globalHistory = history;
  }

  componentWillReceiveProps(nextProps) {
    globalHistory = nextProps.history;
  }

  render() {
    return null;
  }
}

export const GlobalHistory = withRouter(HistoryComponent);

export default function gotoRoute(route) {
  return globalHistory.push(route);
}

And then import into your component:

import gotoRoute from "../../history";

gotoRoute({
                        pathname: "/your_url_here",
                        state: {
                            id: this.state.id
                        }
                    });

And in index.js

import {GlobalHistory} from "./history";

ReactDOM.render((
  <Provider store={store}>
    <BrowserRouter >
      <div>
        <GlobalHistory/>
        <App/>
      </div>
    </BrowserRouter>
  </Provider>

), document.getElementById('root'));
dalvir
  • 252
  • 4
  • 8