React-Router-Dom v6 保护路由

Posted

技术标签:

【中文标题】React-Router-Dom v6 保护路由【英文标题】:React-Router-Dom v6 protected routes 【发布时间】:2021-11-03 21:24:32 【问题描述】:

我需要有关如何使用 react-router-dom v6 实现受保护路由的帮助。这个例子对我不起作用。我想在访问受保护的路由和未登录的用户时,重定向到登录页面。

AuthenticatedRoute

import React from "react";
import  Route, Navigate  from "react-router-dom";

const AuthenticatedRoute = ( component: Component, ...rest ) => 
  //converts object to boolean ->false if null else true//
  const isAuthenticated = !!localStorage.getItem("token");
  return (
    <Route
      ...rest
      render=(props) =>
        isAuthenticated ? (
          <Component ...props />
        ) : (
          <Navigate to="/auth/login" />
        )
      
    />
  );
;

export default React.memo(AuthenticatedRoute);

Routes.js

import React,  lazy  from "react";

import  Navigate, Route, Routes  from "react-router-dom";
import AuthenticatedRoute from "./Components/Routes/AuthenticatedRoute";


const AppRoutes = () => 
 
  return (
    <Routes>
      <AuthenticatedRoute
        path="/admin"
        component=<Navigate to="/admin/dashboard" />
      />

      <AuthenticatedRoute exact path="/admin" component=<DashboardLayout />>
        <AuthenticatedRoute exact path="/dashboard" component=<Dashboard /> />
        <AuthenticatedRoute
          exact
          path="/property-management"
          component=<AdminPropManagement />
        />
        <AuthenticatedRoute
          exact
          path="/new-property"
          component=<NewProperty />
        />
      </AuthenticatedRoute>
    </Routes>
  );
;

export default AppRoutes;

【问题讨论】:

【参考方案1】:

经过深入研究,这对我有用。

注意:

    redirect 在 react-router-dom v6 中被 Navigate 取代 在 react-router-dom v6 中,Switch 被替换为 Routes

Routes.js

import  Navigate, Route, Routes  from "react-router-dom";

const AppRoutes = () => 

  const isAuthenticated = !!localStorage.getItem("token");

  return (
    <Routes>
      <Route
        path="/admin"
        element=
          isAuthenticated ? (
            <Navigate to="/admin/dashboard" />
          ) : (
            <Navigate to="/auth/login" />
          )
        
      />

      <Route
        exact
        path="/admin"
        element=
          isAuthenticated ? <DashboardLayout /> : <Navigate to="/auth/login" />
        
      >
        <Route exact path="/dashboard" element=<Dashboard /> />
        <Route
          exact
          path="/property-management"
          element=<AdminPropManagement />
        />
        <Route exact path="/new-property" element=<NewProperty /> />
      </Route>

    </Routes>
  );
;

export default AppRoutes;

【讨论】:

【参考方案2】:

我不知道&lt;Navigate /&gt;,以前也没有使用过,但我正在使用并为我工作的是&lt;Redirect to=path /&gt;useHistory 钩子。

    重定向

import React from "react";
import  Route, Redirect  from "react-router-dom";

const AuthenticatedRoute = ( component: Component, ...rest ) => 
  //converts object to boolean ->false if null else true//
  const isAuthenticated = !!localStorage.getItem("token");
  return (
    <Route
      ...rest
      render=(props) =>
        isAuthenticated ? (
          <Component ...props />
        ) : (
          <Redirect to="/auth/login" />
        )
      
    />
  );
;

export default React.memo(AuthenticatedRoute);
    使用历史

import React from "react";
import  Route, Redirect, useHistory  from "react-router-dom";

const AuthenticatedRoute = ( component: Component, ...rest ) => 
  //converts object to boolean ->false if null else true//
  const isAuthenticated = !!localStorage.getItem("token");
  const history = useHistory();
  
  if(!isAuthenticated) 
    history.push("/auth/login");
  
  
  return (
    <Route
      ...rest
      render=(props) => <Component ...props />
    />
  );
;

export default React.memo(AuthenticatedRoute);

【讨论】:

谢谢。这很有帮助,但 Redirect 在 react-router-dom v6 中被 Navigate 取代【参考方案3】:

试试这个 -

import  Redirect  from "react-router-dom";

     render() 
        return this.state.isAuthenticated ? (
                <Component/>
            ):(
          <Redirect to= pathname: "/web/login"  />
        );
      
    

【讨论】:

谢谢。这很有帮助,但 Redirect 在 react-router-dom v6 中被 Navigate 取代

以上是关于React-Router-Dom v6 保护路由的主要内容,如果未能解决你的问题,请参考以下文章

如何从 react-router-dom v6 中的其他位置/功能获取路由?

React-router-dom 受保护的路由不起作用

使用反应路由器 v6 的受保护路由

页面未呈现;在 react-router-dom 中使用受保护的路由将道具传递给孩子时

使用 react router dom v6 保护路由 渲染没有返回任何内容

如何在 react-router-dom 中实现像 vue-router 这样的路由器离开保护?