ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[Snippet] - react router authentication

2022-01-19 22:02:48  阅读:222  来源: 互联网

标签:react Authentication privateRoutes publicRoutes Snippet authentication tsx impor


同步链接: https://www.shanejix.com/posts/[Snippet] - react router authentication/

file tree

.
├── conponents
    └── Authentication.tsx
├── pages
    ├── Home.tsx
    ├── Login.tsx
    └── Management.tsx
├── routes
    ├── privateRoutes.tsx
    └── publicRoutes.tsx
├── App.tsx

App.tsx

import React, { useState } from "react";
import { BrowserRouter as Router, Switch, Route } from "react-router-dom";
import publicRoutes from "./routes/publicRoutes";
import privateRoutes from "./routes/privateRoutes";
import Authentication from "./components/Authentication";

function App() {
  const [user, setUser] = useState({});

  const loginAsUser = () => {
    setUser({
      role: ["user"],
    });
  };

  return (
    <Router>
      <Switch>
        {publicRoutes.map(({ path, component: Component, ...route }) => (
          <Route
            key={path}
            path={path}
            render={(routeProps: any) => (
              <Component loginAsUser={loginAsUser} {...routeProps} />
            )}
            {...route}
          />
        ))}
        {privateRoutes.map((route) => (
          <Authentication key={route.path} {...route} user={user} />
        ))}
      </Switch>
    </Router>
  );
}

export default App;

publicRoutes.ts

import Login from "../pages/Login";
import Home from "../pages/Home";

const publicRoutes = [
  {
    path: "/login",
    component: Login,
    exact: true,
  },
  {
    path: "/",
    component: Home,
    exact: true,
  },
];

export default publicRoutes;

privateRoutes.ts

import Management from "../pages/Management";

const privateRoutes = [
  {
    path: "/management",
    component: Management,
    exact: true,
    role: "user",
    backUrl: "/login",
  },
];

export default privateRoutes;

Authentication.tsx

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

function Authentication(props: any) {
  const {
    user: { role: userRole },
    role: routeRole,
    backUrl,
    ...otherProps
  } = props;

  // 如果用户有权限,就渲染对应的路由
  if (userRole && userRole.includes(routeRole)) {
    return <Route {...otherProps} />;
  } else {
    // 如果没有权限,返回配置的默认路由
    return <Redirect to={backUrl} />;
  }
}

export default Authentication;

注意:react-router-dom 所依赖的版本为 "^5.3.3"

作者:shanejix
出处:https://www.shanejix.com/posts/[Snippet] - react router authentication/
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
声明:转载请注明出处!

标签:react,Authentication,privateRoutes,publicRoutes,Snippet,authentication,tsx,impor
来源: https://www.cnblogs.com/shanejix/p/15824308.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有