【问题标题】:React PrivateRoute authentication and unresolved token promise issueReact PrivateRoute 身份验证和未解决的令牌承诺问题
【发布时间】:2020-09-15 13:11:30
【问题描述】:

我有一个想要为其创建 PrivateRoutes 的 react 应用程序。以下是PrivateRoute.tsx文件:

import * as React from 'react';
import { Route,Redirect, RouteProps} from 'react-router-dom';
import { getToken } from '../../auth/auth';

interface PrivateRouteProps extends RouteProps {
  component?: any;
  children?: any;
}

const PrivateRoute = (props: PrivateRouteProps) => {
  const { component: Component, children, ...rest } = props;
  const [isAuthenticated, setAuth ] = useState(false);

  useEffect(() => {
    async function fetchToken() {
      try {
        const token = await getToken();
    
        if (token && token.length) {
          setAuth(true);
        } else {
          setAuth(false);
        }
       } catch(e) {
         console.log(e);
         setAuth(false);
       }
    }
    fetchToken();
  }, [isAuthenticated]);

  return (
    <Route
      {...rest}
      render={routeProps =>
        isAuthenticated ? (
          Component ? (
            <Component {...routeProps} />
          ) : (
            children
          )
        ) : (
          <Redirect
            to={{
              pathname: '/signin',
              state: { from: routeProps.location },
            }}
          />
        )
      }
    />
  );
};

export default PrivateRoute;

以下是我在 App.tsx 文件中的路由设置

//...
const App = () => {
  return (
    <div className="application">
        <Router>
          <Switch>
            <Route path="/signin" component={SignIn} />
            <Route path="/signup" component={SignUp} />
            <NavBar>
              <Switch>
                <PrivateRoute path="/tasks" component={Tasks}/>
                <PrivateRoute path="/people" component={People}/>
              </Switch>
            </NavBar>
            <Route component={NotFound} />
            <Redirect from="/" to="/signin" exact={true} />
          </Switch>
        </Router>
    </div>
  );
};

export default App;

令牌存储在 LocalForage 中。上面的代码产生以下警告,我无法访问 PrivateRoutes。我怀疑这是因为组件在承诺得到解决并且令牌可用之前被返回。

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

我会感谢有用的答案。

【问题讨论】:

    标签: reactjs react-router react-router-dom react-typescript


    【解决方案1】:

    尝试在令牌获取未完成时添加某种加载器,因为您的 PrivateRoute 立即返回 Redirect 将您重定向到另一个组件,但由于您尚未取消您的承诺,一旦完成它将调用 @ PrivateRoute 上的 987654323@ 实际上并未安装,它会导致您遇到此问题。

    例子:

    import * as React from 'react';
    import { Route,Redirect, RouteProps} from 'react-router-dom';
    import { getToken } from '../../auth/auth';
    
    interface PrivateRouteProps extends RouteProps {
      component?: any;
      children?: any;
    }
    
    const PrivateRoute = (props: PrivateRouteProps) => {
      const { component: Component, children, ...rest } = props;
      const [isAuthenticated, setAuth ] = useState(false);
      const [isAuthenticating, setIsAuthenticating] = useState(true);
    
      useEffect(() => {
        async function fetchToken() {
          try {
            setIsAuthenticating(true);
            const token = await getToken();
        
            if (token && token.length) {
              setAuth(true);
            } else {
              setAuth(false);
            }
           } catch(e) {
             console.log(e);
             setAuth(false);
           } finally {
               setIsAuthenticating(false);
           }
        }
        fetchToken();
      }, [isAuthenticated]);
    
      if (isAuthenticating) {
          return <span>Loading...</span>
      }
    
      return (
        <Route
          {...rest}
          render={routeProps =>
            isAuthenticated ? (
              Component ? (
                <Component {...routeProps} />
              ) : (
                children
              )
            ) : (
              <Redirect
                to={{
                  pathname: '/signin',
                  state: { from: routeProps.location },
                }}
              />
            )
          }
        />
      );
    };
    
    export default PrivateRoute;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2021-10-15
      • 2017-01-19
      • 1970-01-01
      • 2019-08-04
      • 1970-01-01
      相关资源
      最近更新 更多