|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import Image from "next/image"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { useRouter, useSearchParams } from "next/navigation"; |
| 6 | +import { useState } from "react"; |
| 7 | +import { env } from "next-runtime-env"; |
| 8 | + |
| 9 | +export default function VerifyOTP() { |
| 10 | + const [formData, setFormData] = useState({ |
| 11 | + otp: "", |
| 12 | + newPassword: "", |
| 13 | + confirmPassword: "", |
| 14 | + }); |
| 15 | + const [isLoading, setIsLoading] = useState(false); |
| 16 | + const searchParams = useSearchParams(); |
| 17 | + const email = searchParams.get("m"); |
| 18 | + const router = useRouter(); |
| 19 | + |
| 20 | + const handleChange = (e) => { |
| 21 | + setFormData({ ...formData, [e.target.name]: e.target.value }); |
| 22 | + }; |
| 23 | + |
| 24 | + const handleSubmit = async (e) => { |
| 25 | + e.preventDefault(); |
| 26 | + |
| 27 | + // Validate password match |
| 28 | + if (formData.newPassword !== formData.confirmPassword) { |
| 29 | + alert("Passwords do not match. Please try again."); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + setIsLoading(true); |
| 34 | + |
| 35 | + const inputData = { |
| 36 | + email: email, |
| 37 | + otp: formData.otp, |
| 38 | + new_password: formData.newPassword, |
| 39 | + }; |
| 40 | + |
| 41 | + try { |
| 42 | + const response = await fetch( |
| 43 | + (env("NEXT_PUBLIC_AUTH_BASE_URL") ?? "http://localhost:5000") + |
| 44 | + "/api/password/reset/verify", |
| 45 | + { |
| 46 | + method: "POST", |
| 47 | + headers: { |
| 48 | + "Content-Type": "application/json", |
| 49 | + }, |
| 50 | + credentials: "include", |
| 51 | + body: JSON.stringify(inputData), |
| 52 | + }, |
| 53 | + ); |
| 54 | + |
| 55 | + if (response.ok) { |
| 56 | + alert("Password reset successful!"); |
| 57 | + setIsLoading(false); |
| 58 | + router.push("/auth"); |
| 59 | + } else { |
| 60 | + let errorData = { |
| 61 | + message: `Verification failed with status: ${response.status}`, |
| 62 | + }; |
| 63 | + try { |
| 64 | + errorData = await response.json(); |
| 65 | + } catch (parseError) { |
| 66 | + console.error( |
| 67 | + "Could not parse error response:", |
| 68 | + parseError, |
| 69 | + ); |
| 70 | + } |
| 71 | + alert( |
| 72 | + errorData.message ?? "Invalid OTP or verification failed.", |
| 73 | + ); |
| 74 | + setIsLoading(false); |
| 75 | + } |
| 76 | + } catch (error) { |
| 77 | + console.error("OTP Verification request failed:", error); |
| 78 | + alert( |
| 79 | + "Failed to connect to the server. Please check your network or try again later.", |
| 80 | + ); |
| 81 | + setIsLoading(false); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + return ( |
| 86 | + <div className="flex flex-col items-center min-h-screen font-[family-name:var(--font-geist-mono)] p-8 bg-gray-100"> |
| 87 | + <div className="flex items-center justify-center overflow-hidden h-32"> |
| 88 | + <Image |
| 89 | + src="/LOGO.png" |
| 90 | + alt="EVOLVE OnClick logo" |
| 91 | + height={320} |
| 92 | + width={680} |
| 93 | + /> |
| 94 | + </div> |
| 95 | + <div className="flex flex-col flex-grow min-w-[32%] w-full md:max-w-[80%] lg:max-w-[60%] xl:max-w-[40%] mx-auto justify-center items-center align-middle"> |
| 96 | + <form |
| 97 | + onSubmit={handleSubmit} |
| 98 | + className="flex flex-col gap-1 p-4 w-full bg-white shadow-sm border border-dashed border-gray-200 rounded-3xl" |
| 99 | + > |
| 100 | + <div className="mb-4"> |
| 101 | + <h2 className="text-xl font-semibold text-center"> |
| 102 | + Verify OTP |
| 103 | + </h2> |
| 104 | + <p className="text-xs text-gray-500 text-center break-words px-2"> |
| 105 | + {`Please enter the OTP sent to ${email ?? "your email"}.`} |
| 106 | + </p> |
| 107 | + <p className="text-xs text-gray-500 text-center break-words px-2 mt-2 mx-auto justify-center items-center align-middle"> |
| 108 | + OTP will be sent only if the email is registered. If |
| 109 | + you did not receive an OTP, please check the email |
| 110 | + ID you typed again and{" "} |
| 111 | + <Link |
| 112 | + href="/auth/recover" |
| 113 | + className="text-blue-600 hover:text-blue-800 underline cursor-pointer" |
| 114 | + > |
| 115 | + retry |
| 116 | + </Link> |
| 117 | + . |
| 118 | + </p> |
| 119 | + </div> |
| 120 | + <input |
| 121 | + type="text" |
| 122 | + name="otp" |
| 123 | + placeholder="Enter OTP" |
| 124 | + value={formData.otp} |
| 125 | + onChange={handleChange} |
| 126 | + className="w-full p-2 mt-4 border rounded-xl" |
| 127 | + required |
| 128 | + pattern="\d*" |
| 129 | + maxLength={6} |
| 130 | + inputMode="numeric" |
| 131 | + autoComplete="one-time-code" |
| 132 | + disabled={isLoading} |
| 133 | + /> |
| 134 | + <input |
| 135 | + type="password" |
| 136 | + name="newPassword" |
| 137 | + placeholder="Enter new password" |
| 138 | + value={formData.newPassword} |
| 139 | + onChange={handleChange} |
| 140 | + className="w-full p-2 mt-4 border rounded-xl" |
| 141 | + required |
| 142 | + disabled={isLoading} |
| 143 | + /> |
| 144 | + <input |
| 145 | + type="password" |
| 146 | + name="confirmPassword" |
| 147 | + placeholder="Confirm new password" |
| 148 | + value={formData.confirmPassword} |
| 149 | + onChange={handleChange} |
| 150 | + className="w-full p-2 mt-4 border rounded-xl" |
| 151 | + required |
| 152 | + disabled={isLoading} |
| 153 | + /> |
| 154 | + <button |
| 155 | + type="submit" |
| 156 | + className={`rounded-full transition-colors flex items-center justify-center bg-yellow-400 text-black hover:bg-yellow-50 text-sm sm:text-base p-2 w-full border border-black gap-2 mt-4 ${ |
| 157 | + isLoading ? "opacity-50 cursor-not-allowed" : "" |
| 158 | + }`} |
| 159 | + disabled={isLoading} |
| 160 | + > |
| 161 | + {isLoading ? "Verifying..." : "Verify OTP"} |
| 162 | + </button> |
| 163 | + </form> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + ); |
| 167 | +} |
0 commit comments