
与后端部分相比,前端部分非常简单。我需要做的就是创建一个模式,并使用它发送数据两次。
为了创建模式,我从我早期项目 chat-nat 的 messagemodal 组件中复制了一些代码,即用于封装模式的类名。
我会添加“忘记密码?”登录页面上的按钮,并设置 onclick 处理程序以打开模态
在请求之前,我需要使用布尔状态来表示 otp 是否已发送到用户的电子邮件。我将状态命名为 otpsent
立即学习“前端免费学习笔记(深入)”;
1、演示:以截图为准 2、程序说明 程序试用后台:http://你的域名/admin/login.asp 后台登陆帐号:admin 密码:admin123 说明: 这个是基于asp+access的企业网站源码,数据库已设有有防下载,网站更安全 要修改网站,自定义你自己要的页面,和美化页面都是你自己完成,网站源码程序完整,后台功能强大。 调试运行环境:要安装IIS服务器(IIS的安装和配置,安装好
0
以下是我从该项目的现有前端重用的一些组件和挂钩:
这是模态的完整代码:
// src/components/passwordresetmodal.tsx
import react, { usestate } from "react"
import authform from "./authform";
import forminput from "./forminput";
import box from "./box";
import { useaxios } from "../hooks/useaxios";
interface formdata {
email: string,
new_password: string,
otp: string,
}
interface props {
isvisible: boolean,
onclose: () => void,
}
const passwordresetmodal: react.fc<props> = ({ isvisible, onclose }) => {
const [formdata, setformdata] = usestate<formdata>({
email: "",
new_password: "",
otp: ""
});
const [isloading, setloading] = usestate<boolean>(false);
const [isotpsent, setotpsent] = usestate<boolean>(false);
const { apireq } = useaxios();
const handleclose = (e: react.mouseevent<htmldivelement, mouseevent>) => {
if ((e.target as htmlelement).id === "wrapper") {
onclose();
// could have setotpsent(false), but avoiding it in case user misclicks outside
}
};
const handlechange = (e: react.changeevent<htmlinputelement>) => {
const { name, value } = e.target;
setformdata({
...formdata,
[name]: value,
});
};
const handlesubmit = async (e: react.formevent<htmlformelement>) => {
e.preventdefault();
setloading(true);
if (!isotpsent) { // first request for sending otp,
const response = await apireq<unknown, formdata>("post", "/api/reset-password", formdata)
if (response) {
alert("otp has been sent to your email");
setotpsent(true);
}
} else { // then using otp to change password
const response = await apireq<unknown, formdata>("put", "/api/reset-password", formdata)
if (response) {
alert("password has been successfully reset\nplease log in again");
// clear the form
setformdata({
email: "",
otp: "",
new_password: "",
})
// close modal
onclose();
}
}
setloading(false);
};
if (!isvisible) return null;
return (
<div
id="wrapper"
classname="fixed inset-0 bg-black bg-opacity-25 backdrop-blur-sm flex justify-center items-center"
onclick={handleclose}>
<box title="password reset">
<authform
submithandler={handlesubmit}
isloading={isloading}
buttontext={isotpsent ? "change password" : "send otp"}>
<forminput
id="email"
label="your email"
type="email"
value={formdata.email}
changehandler={handlechange}
isrequired />
{isotpsent && (<>
<forminput
id="otp"
label="otp"
type="text"
value={formdata.otp}
changehandler={handlechange}
isrequired />
<forminput
id="new_password"
label="new password"
type="password"
value={formdata.new_password}
changehandler={handlechange}
isrequired />
</>)}
</authform>
</box>
</div>
)
}
export default passwordresetmodal
这是在登录表单中处理模式的条件渲染的方式
// src/pages/auth/Login.tsx
import PasswordResetModal from "../../components/PasswordResetModal";
const Login: React.FC = () => {
const [showModal, setShowModal] = useState<boolean>(false);
return (
<Section>
<Box title="Login">
<div className="grid grid-flow-col">
{/* link to the register page here */}
<button
type="button"
onClick={() => setShowModal(true)}
className="text-blue-700 hover:text-white border border-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-3 py-2 text-center me-2 mb-2 dark:border-blue-500 dark:text-blue-500 dark:hover:text-white dark:hover:bg-blue-500 dark:focus:ring-blue-800">
Forgot Password?
</button>
<PasswordResetModal isVisible={showModal} onClose={() => setShowModal(false)} />
</div>
</Box>
</Section>
)
我们完成了!至少我是这么想的。
在我的开发环境中运行该应用程序时,我发现了一个错误,如果后端已经运行很长时间,则电子邮件将无法通过。
我们将在下一篇文章中修复此错误
以上就是密码重置功能:前端的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号