From 3020a2ef3b317c7751b613bee87648539f671ed7 Mon Sep 17 00:00:00 2001 From: Elijah Ngo <161224816+Techngo10@users.noreply.github.com> Date: Fri, 26 Jul 2024 20:34:12 +0930 Subject: [PATCH] [Account Already Exist]: --- StepOne.tsx | 207 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 StepOne.tsx diff --git a/StepOne.tsx b/StepOne.tsx new file mode 100644 index 00000000..2c779fb2 --- /dev/null +++ b/StepOne.tsx @@ -0,0 +1,207 @@ +import Button from '@/components/Button'; +import ControlledField from '@/components/ControlledField'; +import { useSignUp } from '@clerk/nextjs'; +import { zodResolver } from '@hookform/resolvers/zod'; +import Link from 'next/link'; +import { useRouter } from 'next/navigation'; +import { useState } from 'react'; +import { useForm } from 'react-hook-form'; +import { FcGoogle } from 'react-icons/fc'; +import { z } from 'zod'; +import { handleClerkErrors } from '../../helpers'; +import { + codeSchema, + emailSchema, + firstNameSchema, + lastNameSchema, + passwordSchema, +} from '../../schemas'; +import { useSetJoinUsHeading } from '../store'; + +const verifyEmailSchema = z.object({ code: codeSchema }); +function VerifyEmail() { + useSetJoinUsHeading({ + title: 'Verify your email', + description: 'Please enter the verification code that was sent to your email', + }); + + const form = useForm>({ + defaultValues: { code: '' }, + resolver: zodResolver(verifyEmailSchema), + }); + + const { isLoaded, signUp, setActive } = useSignUp(); + + const [verifyEmailLoading, setVerifyEmailLoading] = useState(false); + + const router = useRouter(); + const handleVerify = form.handleSubmit(async ({ code }) => { + if (!isLoaded) return; + + setVerifyEmailLoading(true); + + try { + const completeSignUp = await signUp.attemptEmailAddressVerification({ + code, + }); + if (completeSignUp.status !== 'complete') { + // Investigate the response, to see if there was an error or if the user needs to complete more steps. + console.log(JSON.stringify(completeSignUp, null, 2)); + return; + } + await setActive({ session: completeSignUp.createdSessionId }); + router.refresh(); + } catch (error) { + handleClerkErrors(error, form, [ + { code: 'form_param_nil', field: 'code', message: 'Please enter the the code.' }, + { + code: 'form_code_incorrect', + field: 'code', + message: 'Incorrect Code. Please enter the code from your email.', + }, + ]); + } + + setVerifyEmailLoading(false); + }); + + return ( +
+
+ + + +
+ ); +} + +const stepOneSchema = z.object({ + firstName: firstNameSchema, + lastName: lastNameSchema, + emailAddress: emailSchema, + password: passwordSchema, +}); + +export default function StepOne() { + useSetJoinUsHeading({ + title: 'Join Us', + description: 'Create your account', + }); + + const form = useForm>({ + defaultValues: { firstName: '', lastName: '', emailAddress: '', password: '' }, + resolver: zodResolver(stepOneSchema), + }); + + const { signUp, isLoaded } = useSignUp(); + const [pendingVerification, setPendingVerification] = useState(false); + + const [emailJoinLoading, setEmailJoinLoading] = useState(false); + + const handleSignUp = form.handleSubmit(async (formData) => { + if (!isLoaded) return; + + setEmailJoinLoading(true); + + try { + await signUp.create(formData); + await signUp.prepareEmailAddressVerification({ strategy: 'email_code' }); + // Change the UI to our pending section. + setPendingVerification(true); + } catch (error) { + handleClerkErrors(error, form, [ + { + code: 'form_password_not_strong_enough', + field: 'password', + message: + 'Given password is not strong enough. For account safety, please use a different password.', + }, + { + code: 'form_password_pwned', + field: 'password', + message: + 'Password has been found in an online data breach. For account safety, please use a different password.', + }, + { + code: 'form_email_already_exists', + field: 'emailAddress', + message: + 'The email provided is already connected to an existing account.', + }, + ]); + } + + setEmailJoinLoading(false); + }); + + const handleGoogleSignUp = async () => { + if (!isLoaded) return; + try { + await signUp.authenticateWithRedirect({ + strategy: 'oauth_google', + redirectUrl: '/sso-callback', + redirectUrlComplete: '/join', + }); + } catch (error) { + // Handle any errors that might occur during the sign-up process + console.error('Google Sign-Up Error:', error); + } + }; + + if (pendingVerification) return ; + + return ( +
+ + +
+
+

or

+
+
+ +
+ + + + +
+ +
+ + + {/* Sign-in option */} +
+

+ Already have an account?{' '} + + Sign in + +

+
+
+ ); +}