|
| 1 | +"use client"; |
| 2 | +import { |
| 3 | + Card, |
| 4 | + CardContent, |
| 5 | + CardFooter, |
| 6 | + CardHeader, |
| 7 | + CardTitle, |
| 8 | +} from "@/components/ui/card"; |
| 9 | +import React, { useEffect, useState } from "react"; |
| 10 | +import Logo from "../logo"; |
| 11 | +import { Input } from "@/components/ui/input"; |
| 12 | +import { Label } from "@/components/ui/label"; |
| 13 | +import { Eye, EyeClosed, Loader2 } from "lucide-react"; |
| 14 | +import { Button } from "@/components/ui/button"; |
| 15 | +import Link from "next/link"; |
| 16 | +import { useAuth, useSignIn } from "@clerk/nextjs"; |
| 17 | +import { |
| 18 | + InputOTP, |
| 19 | + InputOTPGroup, |
| 20 | + InputOTPSlot, |
| 21 | +} from "@/components/ui/input-otp"; |
| 22 | +import { useRouter } from "next/navigation"; |
| 23 | +import { showError, showSuccess, showWarning } from "@/lib/sonner"; |
| 24 | + |
| 25 | +const ResetPasswordForm = () => { |
| 26 | + // states |
| 27 | + const [email, setEmail] = useState('') |
| 28 | + const [password, setPassword] = useState('') |
| 29 | + const [code, setCode] = useState('') |
| 30 | + const [successfulCreation, setSuccessfulCreation] = useState(false) |
| 31 | + const [secondFactor, setSecondFactor] = useState(false) |
| 32 | + const [error, setError] = useState('') |
| 33 | + const [showPassword, setShowPassword] = useState(false) |
| 34 | + const [cooldown, setCooldown] = useState(60); |
| 35 | + const [loaders, setLoaders] = useState({ |
| 36 | + createLoader: false, |
| 37 | + resetLoader: false |
| 38 | + }) |
| 39 | + |
| 40 | + |
| 41 | + // hooks |
| 42 | + const router = useRouter() |
| 43 | + const { isSignedIn } = useAuth() |
| 44 | + const { isLoaded, signIn, setActive } = useSignIn() |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + if (isSignedIn) { |
| 48 | + router.push('/') |
| 49 | + } |
| 50 | + }, [isSignedIn, router]) |
| 51 | + |
| 52 | + useEffect(() => { |
| 53 | + if (cooldown <= 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + const timer = setInterval(() => { |
| 57 | + setCooldown((prev) => prev - 1); |
| 58 | + }, 1000); |
| 59 | + |
| 60 | + return () => clearInterval(timer); |
| 61 | + }, [cooldown]); |
| 62 | + |
| 63 | + |
| 64 | + if (!isLoaded) { |
| 65 | + return null |
| 66 | + } |
| 67 | + |
| 68 | + async function create(e: React.FormEvent) { |
| 69 | + e.preventDefault() |
| 70 | + if (!email) { |
| 71 | + showWarning("Email is required") |
| 72 | + return; |
| 73 | + } |
| 74 | + setLoaders((prev) => ({ ...prev, createLoader: true })) |
| 75 | + await signIn |
| 76 | + ?.create({ |
| 77 | + strategy: 'reset_password_email_code', |
| 78 | + identifier: email, |
| 79 | + }) |
| 80 | + .then(() => { |
| 81 | + setSuccessfulCreation(true) |
| 82 | + showSuccess("Code sent to your E-mail") |
| 83 | + setError('') |
| 84 | + }) |
| 85 | + .catch((err) => { |
| 86 | + console.error('error', err.errors[0].longMessage) |
| 87 | + setError(err.errors[0].longMessage) |
| 88 | + showError(err.errors[0].longMessage) |
| 89 | + }).finally(() => setLoaders((prev) => ({ ...prev, createLoader: false }))) |
| 90 | + } |
| 91 | + |
| 92 | + // Reset the user's password. |
| 93 | + // Upon successful reset, the user will be |
| 94 | + // signed in and redirected to the home page |
| 95 | + async function reset(e: React.FormEvent) { |
| 96 | + e.preventDefault() |
| 97 | + setLoaders((prev) => ({ ...prev, resetLoader: true })) |
| 98 | + await signIn |
| 99 | + ?.attemptFirstFactor({ |
| 100 | + strategy: 'reset_password_email_code', |
| 101 | + code, |
| 102 | + password, |
| 103 | + }) |
| 104 | + .then((result) => { |
| 105 | + // Check if 2FA is required |
| 106 | + if (result.status === 'needs_second_factor') { |
| 107 | + setSecondFactor(true) |
| 108 | + setError('') |
| 109 | + } else if (result.status === 'complete') { |
| 110 | + // Set the active session to |
| 111 | + // the newly created session (user is now signed in) |
| 112 | + setActive({ |
| 113 | + session: result.createdSessionId, |
| 114 | + navigate: async ({ session }) => { |
| 115 | + if (session?.currentTask) { |
| 116 | + // Check for tasks and navigate to custom UI to help users resolve them |
| 117 | + // See https://clerk.com/docs/custom-flows/overview#session-tasks |
| 118 | + console.log(session?.currentTask) |
| 119 | + return |
| 120 | + } |
| 121 | + |
| 122 | + router.push('/workplace') |
| 123 | + }, |
| 124 | + }) |
| 125 | + showSuccess("Password reset success. Go to Dashboard!") |
| 126 | + setError('') |
| 127 | + } else { |
| 128 | + console.log(result) |
| 129 | + } |
| 130 | + }) |
| 131 | + .catch((err) => { |
| 132 | + console.error('error', err.errors[0].longMessage) |
| 133 | + setError(err.errors[0].longMessage) |
| 134 | + showError(err.errors[0].longMessage) |
| 135 | + }).finally(() => setLoaders((prev) => ({ ...prev, resetLoader: false }))) |
| 136 | + } |
| 137 | + |
| 138 | + return ( |
| 139 | + <Card> |
| 140 | + <CardHeader> |
| 141 | + <CardTitle className="flex flex-col lg:flex-row text-center justify-center items-center gap-3 text-2xl text-wrap"> |
| 142 | + <span>Reset your password</span> |
| 143 | + <span> |
| 144 | + <Logo textClasses="text-2xl" hideLogo /> |
| 145 | + </span> |
| 146 | + </CardTitle> |
| 147 | + </CardHeader> |
| 148 | + <CardContent> |
| 149 | + <form onSubmit={!successfulCreation ? create : reset}> |
| 150 | + {!successfulCreation && ( |
| 151 | + <div className="flex flex-col justify-center items-center gap-6"> |
| 152 | + <div className="w-full space-y-2"> |
| 153 | + <Label htmlFor="email">Provide your email address</Label> |
| 154 | + <Input |
| 155 | + type="email" |
| 156 | + placeholder="e.g john@doe.com" |
| 157 | + value={email} |
| 158 | + onChange={(e) => setEmail(e.target.value)} |
| 159 | + /> |
| 160 | + </div> |
| 161 | + |
| 162 | + <Button className="cursor-pointer" disabled={loaders.createLoader}>{loaders.createLoader ? <Loader2 className="animate-spin" /> : "Send password reset code"}</Button> |
| 163 | + {error && <p>{error}</p>} |
| 164 | + </div> |
| 165 | + )} |
| 166 | + |
| 167 | + {successfulCreation && ( |
| 168 | + <div className="flex flex-col justify-center items-center gap-6"> |
| 169 | + <div className="relative w-full space-y-2"> |
| 170 | + <Label htmlFor="password">Enter your new password</Label> |
| 171 | + <Input |
| 172 | + placeholder="ssshhhhh....." |
| 173 | + type={showPassword ? "text" : "password"} |
| 174 | + value={password} |
| 175 | + onChange={(e) => { |
| 176 | + e.preventDefault() |
| 177 | + setPassword(e.target.value) |
| 178 | + }} |
| 179 | + required |
| 180 | + /> |
| 181 | + <button |
| 182 | + onClick={(e) => { |
| 183 | + e.preventDefault() |
| 184 | + setShowPassword(prev => !prev) |
| 185 | + }} |
| 186 | + className="absolute right-2 top-1/2 " |
| 187 | + > |
| 188 | + {showPassword ? ( |
| 189 | + <Eye |
| 190 | + className="cursor-pointer" |
| 191 | + color="gray" |
| 192 | + size={20} |
| 193 | + /> |
| 194 | + ) : ( |
| 195 | + <EyeClosed |
| 196 | + className="cursor-pointer" |
| 197 | + color="gray" |
| 198 | + size={20} |
| 199 | + /> |
| 200 | + )} |
| 201 | + </button> |
| 202 | + </div> |
| 203 | + |
| 204 | + <div className="w-full space-y-2"> |
| 205 | + <Label htmlFor="code">Enter the password reset code that was sent to your email</Label> |
| 206 | + <InputOTP |
| 207 | + maxLength={6} |
| 208 | + value={code} |
| 209 | + onChange={setCode} |
| 210 | + > |
| 211 | + <InputOTPGroup> |
| 212 | + <InputOTPSlot index={0} /> |
| 213 | + <InputOTPSlot index={1} /> |
| 214 | + <InputOTPSlot index={2} /> |
| 215 | + <InputOTPSlot index={3} /> |
| 216 | + <InputOTPSlot index={4} /> |
| 217 | + <InputOTPSlot index={5} /> |
| 218 | + </InputOTPGroup> |
| 219 | + </InputOTP> |
| 220 | + </div> |
| 221 | + <Button className="w-full cursor-pointer" disabled={loaders.resetLoader}>{loaders.resetLoader ? <Loader2 className="animate-spin" /> : "Reset"}</Button> |
| 222 | + <Button |
| 223 | + variant={"outline"} |
| 224 | + className="mx-auto cursor-pointer" |
| 225 | + disabled={cooldown > 0 || loaders.resetLoader} |
| 226 | + onClick={(e) => { |
| 227 | + e.preventDefault(); |
| 228 | + create(e); |
| 229 | + setCooldown(60); |
| 230 | + }} |
| 231 | + > |
| 232 | + {cooldown > 0 ? ( |
| 233 | + `Resend verification code in ${cooldown}` |
| 234 | + ) : loaders.resetLoader ? ( |
| 235 | + <Loader2 className="animate-spin" /> |
| 236 | + ) : ( |
| 237 | + "Resend verification code" |
| 238 | + )} |
| 239 | + </Button> |
| 240 | + {error && <p>{error}</p>} |
| 241 | + </div> |
| 242 | + )} |
| 243 | + |
| 244 | + {secondFactor && <p>2FA is required, but this UI does not handle that</p>} |
| 245 | + </form> |
| 246 | + </CardContent> |
| 247 | + |
| 248 | + <CardFooter className="flex flex-col gap-2 justify-center items-center"> |
| 249 | + <section className="my-4"> |
| 250 | + <Link href="/login" className="text-blue-400"> |
| 251 | + Back to login |
| 252 | + </Link> |
| 253 | + </section> |
| 254 | + </CardFooter> |
| 255 | + </Card> |
| 256 | + ); |
| 257 | +}; |
| 258 | + |
| 259 | +export default ResetPasswordForm; |
0 commit comments