Reactjs 如何在不付款的情况下保护从用户到访问的路由?

Reactjs 如何在不付款的情况下保护从用户到访问的路由?,reactjs,Reactjs,我有一个向客户收费的系统。每次成功付款后,我都会使用 history.push(“/receipt/:orderid”) 现在我想知道怎样才能使它受到保护?我不允许用户在不付款的情况下从结帐页面访问它,如xyz.com/receipt/321。目前,任何登录的用户都可以键入URL并访问它。您可以使用npm软件包,该软件包为React Router提供了一个中间件API,允许您在导航调用和路线的最终呈现之间执行复杂的逻辑 $npm install react router guards您可以使用n

我有一个向客户收费的系统。每次成功付款后,我都会使用

history.push(“/receipt/:orderid”)

现在我想知道怎样才能使它受到保护?我不允许用户在不付款的情况下从结帐页面访问它,如
xyz.com/receipt/321
。目前,任何登录的用户都可以键入URL并访问它。

您可以使用npm软件包,该软件包为React Router提供了一个中间件API,允许您在导航调用和路线的最终呈现之间执行复杂的逻辑

$npm install react router guards

您可以使用npm软件包,它为react router提供了一个中间件API,允许您在导航调用和路线的最终呈现之间执行复杂的逻辑


$npm安装react路由器防护装置

您可以将受保护的路由包装到另一个react组件中。 让我们把这个组件命名为
ProtectedRoutes
,现在我们可以深入研究代码了

ProtectedRoutes.js

import React, { useContext, useState, useEffect } from "react";

export function ProtectedRoutes({ children, location }) {
  useEffect(() => {
    //this is where you send a request to the server to check if the user has made the payment
    //and if the response was false redirect the user using history.replace()
  }, [location.pathname])
  return (
    <div>
      {children}
    </div>
  );
}

<ProtectedRoutes>
  <Route path="/product" exact component={Product} />
</ProtectedRoutes>
import React,{useContext,useState,useffect}来自“React”;
导出函数ProtectedRoutes({children,location}){
useffect(()=>{
//这是您向服务器发送请求以检查用户是否已付款的地方
//如果响应为false,则使用history.replace()重定向用户
},[location.pathname])
返回(
{儿童}
);
}
Router.js

import React, { useContext, useState, useEffect } from "react";

export function ProtectedRoutes({ children, location }) {
  useEffect(() => {
    //this is where you send a request to the server to check if the user has made the payment
    //and if the response was false redirect the user using history.replace()
  }, [location.pathname])
  return (
    <div>
      {children}
    </div>
  );
}

<ProtectedRoutes>
  <Route path="/product" exact component={Product} />
</ProtectedRoutes>


通过使用此概念,您可以轻松地保护路由。

您可以将受保护的路由包装到另一个react组件中。 让我们把这个组件命名为
ProtectedRoutes
,现在我们可以深入研究代码了

ProtectedRoutes.js

import React, { useContext, useState, useEffect } from "react";

export function ProtectedRoutes({ children, location }) {
  useEffect(() => {
    //this is where you send a request to the server to check if the user has made the payment
    //and if the response was false redirect the user using history.replace()
  }, [location.pathname])
  return (
    <div>
      {children}
    </div>
  );
}

<ProtectedRoutes>
  <Route path="/product" exact component={Product} />
</ProtectedRoutes>
import React,{useContext,useState,useffect}来自“React”;
导出函数ProtectedRoutes({children,location}){
useffect(()=>{
//这是您向服务器发送请求以检查用户是否已付款的地方
//如果响应为false,则使用history.replace()重定向用户
},[location.pathname])
返回(
{儿童}
);
}
Router.js

import React, { useContext, useState, useEffect } from "react";

export function ProtectedRoutes({ children, location }) {
  useEffect(() => {
    //this is where you send a request to the server to check if the user has made the payment
    //and if the response was false redirect the user using history.replace()
  }, [location.pathname])
  return (
    <div>
      {children}
    </div>
  );
}

<ProtectedRoutes>
  <Route path="/product" exact component={Product} />
</ProtectedRoutes>


通过使用此概念,您可以轻松保护您的路由。

我建议:在订单页面上,您检查令牌,如果用户有权限,则向用户发送数据。

我建议:在订单页面上,您检查令牌,如果用户有权限,则向用户发送数据