使用React Router PrivateRoute处理Refreshtoken

时间:2018-10-17 12:13:36

标签: javascript reactjs asynchronous jwt react-router-v4

我正在使用React-Router V4,我想交出“已认证”的道具来决定是否将用户发送到登录页面或请求的页面:

SELECT Fatture.[Codice Progetto], Sum(Fatture.Fattura) AS [Sum] FROM Fatture GROUP BY Fatture.[Codice Progetto];

PrivateRoute.js

我的App.js看起来像这样:

import React from "react";
import {
  Route,
  Redirect,
} from "react-router-dom";
const PrivateRoute = ({ component: Component, authenticated, ...rest }) => (
    <Route
      {...rest}
      render={props =>
        authenticated ? (
          <Component {...props} />
        ) : (
          <Redirect
            to={{
              pathname: "/login",
              state: { from: props.location }
            }}
          />
        )
      }
    />
)
export default PrivateRoute;

由于Axios调用是异步的,因此组件会在调用完成之前呈现。

如何使渲染器等待令牌被刷新?

1 个答案:

答案 0 :(得分:1)

我将isAuthenticated保持在App组件状态。然后,可以在isAuthenticated调用中使用setState来对结果进行重新渲染。当令牌过期时,这也将很重要。