我最近用React完成了一个项目,只开发了客户端,我使用了现成的API。其中一个功能是密码重置,在开发环境中工作得很好,但是在部署到平台之后(我认为该平台无关紧要,因为 Vercel 和 Netlify 上都出现了同样的问题)。
问题是重置密码的链接发送到用户的电子邮件中,但点击时找不到重置页面,我需要帮助,既要修复错误,又要在以后的项目中找出原因。
这里是存储库,请随时提出更改建议并询问更多详细信息,谢谢:(https://github.com/hermesonbastos/Dogs-React)
api.jsx
export const API_URL = "https://dogsapi.origamid.dev/json";
export function PASSWORD_LOST(body) {
return {
url: API_URL + "/api/password/lost",
options: {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
},
};
}
export function PASSWORD_RESET(body) {
return {
url: API_URL + "/api/password/reset",
options: {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
},
};
}
登录.jsx
import React from "react";
import { Navigate, Route, Routes } from "react-router-dom";
import LoginForm from "./LoginForm";
import LoginCreate from "./LoginCreate";
import LoginPasswordLost from "./LoginPasswordLost";
import LoginPasswordReset from "./LoginPasswordReset";
import { UserContext } from "../../UserContext";
import styles from "./Login.module.css";
import NotFound from "../NotFound";
const Login = () => {
const { login } = React.useContext(UserContext);
if (login === true) return <Navigate to="/conta" />;
return (
<section className={styles.login}>
<div className={styles.forms}>
<Routes>
<Route path="/" element={<LoginForm />} />
<Route path="criar" element={<LoginCreate />} />
<Route path="perdeu" element={<LoginPasswordLost />} />
<Route path="resetar" element={<LoginPasswordReset />} />
<Route path="*" element={<NotFound />} />
</Routes>
</div>
</section>
);
};
export default Login;
登录密码重置.jsx
import React from "react";
import Input from "../Forms/Input";
import Button from "../Forms/Button";
import useForm from "../../Hooks/useForm";
import { PASSWORD_RESET } from "../../api";
import useFetch from "../../Hooks/useFetch";
import Error from "../Helper/Error";
import { useNavigate } from "react-router-dom";
import Head from "../Helper/Head";
const LoginPasswordReset = () => {
const [key, setKey] = React.useState("");
const [login, setLogin] = React.useState("");
const password = useForm();
const { error, loading, request } = useFetch();
const navigate = useNavigate();
React.useEffect(() => {
const params = new URLSearchParams(window.location.search);
const key = params.get("key");
const login = params.get("login");
if (key) setKey(key);
if (login) setLogin(login);
}, []);
async function handleSubmit(event) {
event.preventDefault();
if (password.validate()) {
const { url, options } = PASSWORD_RESET({
login,
key,
password: password.value,
});
const { response } = await request(url, options);
if (response.ok) navigate("/login");
}
}
return (
<section className="animeLeft">
<Head title="Resete a senha" />
<h1 className="title">Resete a Senha</h1>
<form onSubmit={handleSubmit}>
<Input
label="Nova Senha"
type="password"
name="password"
{...password}
/>
{loading ? (
<Button disabled>Resetando...</Button>
) : (
<Button>Resetar</Button>
)}
</form>
<Error error={error} />
</section>
);
};
export default LoginPasswordReset;