diff --git a/app/(auth)/forgot-password/page.tsx b/app/(auth)/forgot-password/page.tsx index 794fc74..742f0fa 100644 --- a/app/(auth)/forgot-password/page.tsx +++ b/app/(auth)/forgot-password/page.tsx @@ -3,7 +3,7 @@ import { Button } from "@/components/ui/button" import { IconInput } from "@/components/ui/icon-input" import { Label } from "@/components/ui/label" -import { resetPassword } from "@/lib/api" +import { resetPassword, sendForgotPasswordCode } from "@/lib/api" import { toApiError } from "@/lib/errors" import { notifyInfo, notifySuccess } from "@/lib/toast" import { standardSchemaResolver } from "@hookform/resolvers/standard-schema" @@ -33,6 +33,7 @@ export default function ForgotPasswordPage() { const { register, handleSubmit, + getValues, trigger, formState: { errors, isSubmitting }, } = useForm({ @@ -49,9 +50,14 @@ export default function ForgotPasswordPage() { const isValid = await trigger("email") if (!isValid) return - // Mock sending code - setCountdown(60) - notifySuccess("验证码已发送到你的邮箱") + try { + const email = getValues("email") + await sendForgotPasswordCode(email) + setCountdown(60) + notifySuccess("验证码已发送到你的邮箱") + } catch (err) { + notifyInfo(toApiError(err).msg) + } } const onSubmit = async (data: z.infer) => { diff --git a/app/(auth)/register/page.tsx b/app/(auth)/register/page.tsx index 7c6e775..d0bc27d 100644 --- a/app/(auth)/register/page.tsx +++ b/app/(auth)/register/page.tsx @@ -4,15 +4,15 @@ import { Button } from "@/components/ui/button" import { Checkbox } from "@/components/ui/checkbox" import { IconInput } from "@/components/ui/icon-input" import { Label } from "@/components/ui/label" -import { register as registerApi } from "@/lib/api" +import { register as registerApi, sendEmailVerificationCode } from "@/lib/api" import { toApiError } from "@/lib/errors" -import { notifyInfo } from "@/lib/toast" +import { notifyInfo, notifySuccess } from "@/lib/toast" import { useAuthStore } from "@/store/auth" import { standardSchemaResolver } from "@hookform/resolvers/standard-schema" import { Eye, EyeOff, Lock, Mail, Shield, User } from "lucide-react" import Link from "next/link" import { useRouter } from "next/navigation" -import { useState } from "react" +import { useEffect, useState } from "react" import { Controller, useForm } from "react-hook-form" import { z } from "zod" @@ -39,16 +39,40 @@ export default function RegisterPage() { const { login: storeLogin } = useAuthStore() const [showPassword, setShowPassword] = useState(false) const [showConfirmPassword, setShowConfirmPassword] = useState(false) + const [countdown, setCountdown] = useState(0) const { register, handleSubmit, control, + getValues, + trigger, formState: { errors, isSubmitting }, } = useForm({ resolver: standardSchemaResolver(registerSchema), defaultValues: { agreeTerms: false }, }) + useEffect(() => { + if (countdown <= 0) return + const timer = setInterval(() => setCountdown((c) => c - 1), 1000) + return () => clearInterval(timer) + }, [countdown]) + + const handleSendCode = async () => { + const isValid = await trigger("email") + if (!isValid) return + + const email = String(getValues("email") ?? "") + + try { + await sendEmailVerificationCode({ email, scene: "register" }) + setCountdown(60) + notifySuccess("验证码已发送到你的邮箱") + } catch (err) { + notifyInfo(toApiError(err).msg) + } + } + const onSubmit = async (data: z.infer) => { try { const user = await registerApi({ @@ -100,8 +124,14 @@ export default function RegisterPage() { {...register("vcode")} /> - {errors.vcode &&

{errors.vcode.message}

}