英文:
Long path fails to load with react-router-dom
问题
I have a VerifyEmail route that takes on a JWT token as a parameter. However, it does not load. Is there anyway to get it to load? The route loads if the token is short, about 10 characters long. Anything longer it does not work. I was wondering if there's a limit to the length of the url path for react-router-dom to be causing this problem.
In my App.jsx
import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./components/Home";
import Login from "./components/Login";
import Logout from "./components/Logout";
import SignUp from "./components/SignUp";
import Navbar from "./components/Navbar";
import VerifyEmail from "./features/users/VerifyEmail";
import { selectCurrentToken } from './features/auth/authSlice';
import { useSelector } from "react-redux/es/hooks/useSelector";
function App() {
const auth = useSelector(selectCurrentToken)
return (
<>
<BrowserRouter>
<Navbar auth={auth} />
<Routes>
<Route index element={<Home />} />
{!auth && <Route path="/login" element={<Login />} />}
{auth && <Route path="/logout" element={<Logout />} />}
<Route path="/signup" element={<SignUp />} />
<Route path="/verify/:token" element={<VerifyEmail />} />
</Routes>
</BrowserRouter>
</>
)
}
export default App
In my VerifyEmail.jsx
import React from 'react'
import { useParams } from 'react-router-dom'
const VerifyEmail = () => {
const { token } = useParams()
return (
<div>{token}</div>
)
}
export default VerifyEmail
When I try this link, I get a bad request.
http://localhost:5173/verify/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImUiLCJpYXQiOjE2OTE1NDkwMjcsImV4cCI6MTY5MTU3MDYyN30.HqPtksRbGTxxP8EeFs7XLHfHQLBXleaVil6MYmiKB3Y
(https://i.stack.imgur.com/zhuzb.png)
英文:
I have a VerifyEmail route that takes on a JWT token as a parameter. However, it does not load. Is there anyway to get it to load? The route loads if the token is short, about 10 characters long. Anything longer it does not work. I was wondering if there's a limit to the length of the url path for react-router-dom to be causing this problem.
In my App.jsx
import React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./components/Home"
import Login from "./components/Login"
import Logout from "./components/Logout"
import SignUp from "./components/SignUp"
import Navbar from "./components/Navbar"
import VerifyEmail from "./features/users/VerifyEmail"
import { selectCurrentToken } from './features/auth/authSlice'
import { useSelector } from "react-redux/es/hooks/useSelector";
function App() {
const auth = useSelector(selectCurrentToken)
return (
<>
<BrowserRouter>
<Navbar auth={auth} />
<Routes>
<Route index element={<Home />} />
{!auth && <Route path="/login" element={<Login />} />}
{auth && <Route path="/logout" element={<Logout />} />}
<Route path="/signup" element={<SignUp />} />
<Route path="/verify/:token" element={<VerifyEmail />} />
</Routes>
</BrowserRouter>
</>
)
}
export default App
In my VerifyEmail.jsx
import React from 'react'
import { useParams } from 'react-router-dom'
const VerifyEmail = () => {
const { token } = useParams()
return (
<div>{token}</div>
)
}
export default VerifyEmail
When I try this link, I get a bad request.
http://localhost:5173/verify/eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImUiLCJpYXQiOjE2OTE1NDkwMjcsImV4cCI6MTY5MTU3MDYyN30.HqPtksRbGTxxP8EeFs7XLHfHQLBXleaVil6MYmiKB3Y
答案1
得分: 0
网络服务器错误地将您的JWT与文件混淆。它如何区分localhost:5173/favicon.ico
和localhost:5173/U3MDYyN30.HqPtksRbGTxxP8EeFs7
之间的区别?因此,它会抛出一个Not Found
错误,就像您在图像中所显示的那样。您有两种处理方法:
- 如果您正在使用webpack,您可以配置以禁用点规则(文档)。
historyApiFallback: {
disableDotRule: true
}
- 您可以修改路由并将JWT作为查询参数传递
<Route path="/verify" element={<VerifyEmail />} />
然后在您的VerifyEmail组件中,使用useQuery来获取token的值
// http://localhost:5173/verify?token=abcxyx
import { useQuery } from 'react-router-dom';
function VerifyEmail() {
let query = useQuery();
let token = query.get('token');
// 您组件逻辑的其余部分
}
英文:
The web server is misinterpret your jwt with a file. How is it know the difference between locahost:5173/favicon.ico
and localhost:5173/U3MDYyN30.HqPtksRbGTxxP8EeFs7
. So it will throw an error Not Found
as you show in the image. You have two options to handle this:
- If you are using webpack, you can config to disable the dot rule (docs).
historyApiFallback: {
disableDotRule: true
}
- You can modify the route and pass the JWT as a query parameter
<Route path="/verify" element={<VerifyEmail />} />
And in your VerifyEmail component, use useQuery to get the value of token
// http://localhost:5173/verify?token=abcxyx
import { useQuery } from 'react-router-dom';
function VerifyEmail() {
let query = useQuery();
let token = query.get('token');
// Rest of your component logic
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论