简体   繁体   English

重新路由未使用React登录的用户的干净方法?

[英]Clean way to re-route user that isn't logged in with React?

I guess I could always add login in WillTransitionTo which would do a call to the store via Redux to verify that the user is logged in but this seems a bit hacky, especially since since I have to add WillTransitionTo() logic to every single component. 我想我总是可以在WillTransitionTo中添加登录名,该登录WillTransitionTo通过Redux调用存储以验证用户是否已登录,但这似乎有点麻烦,尤其是因为我必须将WillTransitionTo()逻辑添加到每个组件中。

Anyone have any better ideas? 有人有更好的主意吗? I'm going to work toward wiring something into isAuthorized with the onEnter . 我将努力通过isAuthorized将某些东西连接到onEnter

This is the root root of the application which performs the react-router wire-up. 这是执行反应路由器连线的应用程序的根目录。

react-router : 2.0.4 反应路由器:2.0.4

react: 15.0.2 反应:15.0.2

const store = configureStore();


function isAuthorized() {

}

render(
    <Provider store={store}>
     <Router history={browserHistory}>

        <Route path="/" component={Application} onEnter={isAuthorized}>
            <IndexRoute component={DashboardPage} />
            <Route path="login" component={LoginPage} />
            <Route path="dashboard" component={DashboardPage} />
            <Route path="items" component={DashboardPage} />
            <Route path="item-categories" component={DashboardPage} />
            <Route path="modifiers" component={DashboardPage} />
            <Route path="taxes" component={DashboardPage} />
            <Route path="discounts" component={DashboardPage} />
            <Route path="orders" component={DashboardPage} />
            <Route path="users" component={DashboardPage} />
        </Route>
     </Router>
    </Provider>,
    document.getElementById("application")
);

Note that the example in auth-flow utilizes localStorage to persist userAccess tokens, etc. I really don't want to use localStorage or cookies at this time. 请注意,auth-flow中的示例利用localStorage来保留userAccess令牌等。我真的不希望此时使用localStorage或cookie。 I want to minimize the complexity and store the login state in the store(state.user). 我想最小化复杂性,并将登录状态存储在store(state.user)中。 I don't specifically want to go into the reason for this other than to say that there is a large amount of information that comes the authentication step that needs to be persisted to the store. 除了要说身份验证步骤中包含大量信息需要将其持久存储到商店之外,我不想特别地说明原因。

You can try doing something like 您可以尝试做类似的事情

<Route
  path="/"
  component={Application}
  onEnter={ (nextState, replace) => { isAuthorized(nextState, replace, store) } } >

And in your isAuthorized function 并在您的isAuthorized函数中

function isAuthorized( nextState, replace, store ) {
  // Now you have access to the store, you can
  const state = store.getState();

  // your logic to validate whether the user is logged in or not
  if ( state.user && state.user.loggedIn === true ) {
    // this user is authenticated
    // continue..
  }
  else {
    // unauthenticated user, redirect the user to login page
    replace({
      pathname: '/login',
      state: { nextPathname: nextState.location.pathname }
    });

  }

}

(Posted solution on behalf of the OP) . (代表OP发布解决方案)

This solution worked for me which is a slight modification on the answer: 这个解决方案对我有用,对答案做了些微修改:

function isAuthorized(nextState, replace) {
    if (nextState.location.pathname === "/login") {
        return;
    }
    const state = store.getState();
    if (state.user && state.user.loggedIn === true) {
        return;
    }
    replace("/login");
}


render(
    <Provider store={store}>
        <Router history={browserHistory}>
            <Route path="/" component={Application} onEnter={(nextState, replace) => isAuthorized(nextState, replace)}>
                <IndexRoute component={DashboardPage} />
                <Route path="login" component={LoginPage} />
                <Route path="dashboard" component={DashboardPage} />
                <Route path="items" component={DashboardPage} />
                <Route path="item-categories" component={DashboardPage} />
                <Route path="modifiers" component={DashboardPage} />
                <Route path="taxes" component={DashboardPage} />
                <Route path="discounts" component={DashboardPage} />
                <Route path="orders" component={DashboardPage} />
                <Route path="users" component={DashboardPage} />
            </Route>
        </Router>
    </Provider >,
    document.getElementById("application")
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM