英文:
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: '/reset/:token*',
element: <ResetSetup />,
}
or
{
path: '/reset/:token',
element: <ResetSetup />,
}
答案1
得分: 1
"/"
字符会分割路径段,所以如果路由路径参数值包含这个字面字符,那么整个值就不会被捕获。路由路径可以微调一下,将 "/reset/"
后的所有内容作为 splat 消耗,例如 "/reset/*"
:
<Route path="/reset/*" element={<ResetSetup />} />
import { useParams } from "react-router-dom";
const ResetSetup = () => {
const { "*": token } = useParams();
return (
...
);
};
这只在令牌值是最终路径段时有效。如果路径是像 "/reset/<token value>/someOtherSegment"
这样的,它就不会起作用。
你可以以 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 (
...
);
};
演示
英文:
The "/"
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 "/reset/"
as a splat, e.g. "/reset/*"
:
<Route path="/reset/*" element={<ResetSetup />} />
import { useParams } from "react-router-dom";
const ResetSetup = () => {
const { "*": 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 "/reset/<token value>/someOtherSegment"
.
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:
<Link to={`/reset/${encodeURIComponent("CfDJ8Gnkh7D2epVEspI7K8JRxtKUjYdzndyduSAhrrg7LjshyYkTwbxzYr3E9CPxBcsPLoocEjplmvaV/Qjug+ez2Kwb0USdwdWMzQm4zJuWiZ1U9+ITrWeUdskiHO9QbdFnfQ/4+ME3TuItAKza0ZrYvi+46f3eiSKVhhe9LhGNWKQRB0KToAMQn7pE1gijIhc5wdPsA+jCCb5ebvDFYIeqo2Yvl/AAuehhpE1mTOWi4alw")}`}>
Reset Password (URI Encoded)
</Link>
<Route path="/reset/:token" element={<ResetSetup />} />
const ResetSetup = () => {
const { token } = useParams();
return (
...
);
};
Demo
答案2
得分: 0
在你的路由路径中添加一个 + 号,如下所示:
{
path: "/reset/:token+",
element:
}
英文:
What about adding a + to your Route path ?
{
path: "/reset/:token+",
element: <ResetSetup />,
}
答案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='/reset/:token'
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='/reset/:token'
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论