React Router参数使用带有斜杠的令牌

huangapple go评论49阅读模式
英文:

React Router Param with token having forward slash

问题

我正在使用Identity Framework生成具有/的密码重置令牌

例如:

http://localhost:3000/reset/CfDJ8Gnkh7D2epVEspI7K8JRxtKUjYdzndyduSAhrrg7LjshyYkTwbxzYr3E9CPxBcsPLoocEjplmvaV/Qjug+ez2Kwb0USdwdWMzQm4zJuWiZ1U9+ITrWeUdskiHO9QbdFnfQ/4+ME3TuItAKza0ZrYvi+46f3eiSKVhhe9LhGNWKQRB0KToAMQn7pE1gijIhc5wdPsA+jCCb5ebvDFYIeqo2Yvl/AAuehhpE1mTOWi4alw

如何设置下面的路由路径以接受完整的字符串作为令牌。下面的方式似乎不起作用

{
    path: '/reset/:token*',
    element: <ResetSetup />,        
}

或

{
    path: '/reset/:token',
    element: <ResetSetup />,        
}
英文:

I am using Identity Framework to generate password reset tokens that have / in the token

e.g.

http://localhost:3000/reset/CfDJ8Gnkh7D2epVEspI7K8JRxtKUjYdzndyduSAhrrg7LjshyYkTwbxzYr3E9CPxBcsPLoocEjplmvaV/Qjug+ez2Kwb0USdwdWMzQm4zJuWiZ1U9+ITrWeUdskiHO9QbdFnfQ/4+ME3TuItAKza0ZrYvi+46f3eiSKVhhe9LhGNWKQRB0KToAMQn7pE1gijIhc5wdPsA+jCCb5ebvDFYIeqo2Yvl/AAuehhpE1mTOWi4alw

How to setup below route path to accept the complete string as token. Below does not seem to work

 {
        path: &#39;/reset/:token*&#39;,
        element: &lt;ResetSetup /&gt;,        
    }

or
 {
        path: &#39;/reset/:token&#39;,
        element: &lt;ResetSetup /&gt;,        
    }

答案1

得分: 1

&quot;/&quot; 字符会分割路径段,所以如果路由路径参数值包含这个字面字符,那么整个值就不会被捕获。路由路径可以微调一下,将 &quot;/reset/&quot; 后的所有内容作为 splat 消耗,例如 &quot;/reset/*&quot;

<Route path="/reset/*" element={<ResetSetup />} />
import { useParams } from "react-router-dom";

const ResetSetup = () => {
  const { "*": token } = useParams();

  return (
    ...
  );
};

这只在令牌值是最终路径段时有效。如果路径是像 &quot;/reset/&lt;token value&gt;/someOtherSegment&quot; 这样的,它就不会起作用。

你可以以 URL 安全的方式对令牌值进行编码,这样整个令牌值就成为单个路径段,这样就允许更多动态路径变化:

<Link to={`/reset/${encodeURIComponent("CfDJ8Gnkh7D2epVEspI7K8JRxtKUjYdzndyduSAhrrg7LjshyYkTwbxzYr3E9CPxBcsPLoocEjplmvaV/Qjug+ez2Kwb0USdwdWMzQm4zJuWiZ1U9+ITrWeUdskiHO9QbdFnfQ/4+ME3TuItAKza0ZrYvi+46f3eiSKVhhe9LhGNWKQRB0KToAMQn7pE1gijIhc5wdPsA+jCCb5ebvDFYIeqo2Yvl/AAuehhpE1mTOWi4alw")}`}>
  重置密码URI 编码
</Link>
<Route path="/reset/:token" element={<ResetSetup />} />
const ResetSetup = () => {
  const { token } = useParams();

  return (
    ...
  );
};

演示

React Router参数使用带有斜杠的令牌

英文:

The &quot;/&quot; character splits path segments, so if a route path parameter value contains this literal character then the entire value won't be captured. The route path can be tweaked a bit to either consume everything after &quot;/reset/&quot; as a splat, e.g. &quot;/reset/*&quot;:

&lt;Route path=&quot;/reset/*&quot; element={&lt;ResetSetup /&gt;} /&gt;
import { useParams } from &quot;react-router-dom&quot;;

const ResetSetup = () =&gt; {
  const { &quot;*&quot;: token } = useParams();

  return (
    ...
  );
};

This obviously only works if the token value is the final path segment. It won't work if the path was something like &quot;/reset/&lt;token value&gt;/someOtherSegment&quot;.

You could encode the token value in a URL-safe way so the entire token value is a single path segment, which would allow for more dynamic path variations:

&lt;Link to={`/reset/${encodeURIComponent(&quot;CfDJ8Gnkh7D2epVEspI7K8JRxtKUjYdzndyduSAhrrg7LjshyYkTwbxzYr3E9CPxBcsPLoocEjplmvaV/Qjug+ez2Kwb0USdwdWMzQm4zJuWiZ1U9+ITrWeUdskiHO9QbdFnfQ/4+ME3TuItAKza0ZrYvi+46f3eiSKVhhe9LhGNWKQRB0KToAMQn7pE1gijIhc5wdPsA+jCCb5ebvDFYIeqo2Yvl/AAuehhpE1mTOWi4alw&quot;)}`}&gt;
  Reset Password (URI Encoded)
&lt;/Link&gt;
&lt;Route path=&quot;/reset/:token&quot; element={&lt;ResetSetup /&gt;} /&gt;
const ResetSetup = () =&gt; {
  const { token } = useParams();

  return (
    ...
  );
};

Demo

React Router参数使用带有斜杠的令牌

答案2

得分: 0

在你的路由路径中添加一个 + 号,如下所示:

{
path: "/reset/:token+",
element: ,
}

英文:

What about adding a + to your Route path ?

 {
    path: &quot;/reset/:token+&quot;,
    element: &lt;ResetSetup /&gt;,        
}

答案3

得分: 0

I had this same issue. What I did was encode/decode the URL part that contains slashes. React-router will take the encoded string as a param where you can decode it later to return the slashes and use as needed

So you can keep:
path=&#39;/reset/:token&#39;

This will mean whatever takes you to this path will need to encode (encodeURIComponent) the token. Then when you useParams to get the token you may need to decode (decodeURIComponent) the token. I say may because I believe react-router will decode it in-flight.

英文:

I had this same issue. What I did was encode/decode the URL part that contains slashes. React-router will take the encoded string as a param where you can decoded it later to return the slashes and use as needed

So you can keep:
path=&#39;/reset/:token&#39;

This will mean what ever takes you to this path will need to encode (encodeURIComponent) the token. Then when you useParams to get the token you may need to decode (decodeURIComponent) the token. I say may because I believe react-router will decode it in-flight

huangapple
  • 本文由 发表于 2023年4月11日 01:45:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定