chore: 密码重置界面

This commit is contained in:
14790897 2024-03-08 19:56:57 +08:00
parent 84239677f5
commit d808aa3195
2 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,35 @@
import { useState } from "react";
import { supabase } from "@/utils/supabaseClient";
const RequestResetPassword = () => {
const [email, setEmail] = useState("");
const handleResetPassword = async () => {
const { data, error } = await supabase.auth.api.resetPasswordForEmail(
email,
{
redirectTo: `${window.location.origin}/reset-password`, // 确保这个URL是你重置密码页面的地址
}
);
if (error) {
alert("Error sending password reset email: " + error.message);
} else {
alert("Please check your email for the password reset link");
}
};
return (
<div>
<input
type="email"
placeholder="Your email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button onClick={handleResetPassword}>Reset Password</button>
</div>
);
};
export default RequestResetPassword;

View File

@ -0,0 +1,41 @@
import { useState } from "react";
import { supabase } from "@/utils/supabaseClient";
import { useRouter } from "next/router";
const ResetPassword = () => {
const [newPassword, setNewPassword] = useState("");
const router = useRouter();
const { access_token } = router.query; // 获取URL中的access_token参数
const handleNewPassword = async () => {
if (!access_token) {
alert("Token is not provided or invalid");
return;
}
const { error } = await supabase.auth.api.updateUser(access_token, {
password: newPassword,
});
if (error) {
alert("Error resetting password: " + error.message);
} else {
alert("Your password has been reset successfully");
router.push("/login"); // 导航到登录页面或其他页面
}
};
return (
<div>
<input
type="password"
placeholder="New password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
/>
<button onClick={handleNewPassword}>Update Password</button>
</div>
);
};
export default ResetPassword;