'use client'; import { OTPInput } from '@/components/blocks/otp-input/otp-input'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Button } from '@/components/ui/button'; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; import { Input } from '@/components/ui/input'; import { authClient } from '@/lib/auth/client'; import { portalClient, portalGraphql } from '@/lib/settlemint/portal'; import { cn } from '@/lib/utils'; import { zodResolver } from '@hookform/resolvers/zod'; import { Loader2 } from 'lucide-react'; import Link from 'next/link'; import type { ComponentPropsWithoutRef } from 'react'; import { useForm } from 'react-hook-form'; import { z } from 'zod'; const signUpSchema = z .object({ email: z.string().email('Please enter a valid email'), password: z.string().min(6, 'Password must be at least 6 characters'), name: z.string().min(1, 'Name is required'), walletPincode: z.string().length(6, 'PIN code must be exactly 6 digits'), walletPincodeConfirm: z.string().length(6, 'PIN code must be exactly 6 digits'), }) .refine((data) => data.walletPincode === data.walletPincodeConfirm, { message: "PIN codes don't match", path: ['walletPincodeConfirm'], }); const SetPinCode = portalGraphql(` mutation SetPinCode($name: String!, $address: String!, $pincode: String!) { createWalletVerification( userWalletAddress: $address verificationInfo: {pincode: {name: $name, pincode: $pincode}} ) { id name parameters verificationType } } `); type SignUpFormData = z.infer; export function SignUpForm({ className, redirectUrl = '/', ...props }: ComponentPropsWithoutRef<'form'> & { redirectUrl?: string }) { const decodedRedirectUrl = decodeURIComponent(redirectUrl); const form = useForm({ resolver: zodResolver(signUpSchema), defaultValues: { email: '', password: '', name: '', walletPincode: '', walletPincodeConfirm: '', }, }); const onSubmit = async (data: SignUpFormData) => { await authClient.signUp.email( { email: data.email, password: data.password, name: data.name, wallet: '', // will be filled in by the hook }, { onSuccess: async () => { try { const session = await authClient.getSession(); if (!session.data?.user.wallet) { form.setError('root', { message: 'No wallet address found', }); return; } await portalClient.request(SetPinCode, { name: data.name, address: session.data.user.wallet, pincode: data.walletPincode, }); // Check user role and redirect accordingly const userRole = session.data.user.role; const isAdminOrIssuer = userRole === 'issuer' || userRole === 'admin'; const adminRedirect = decodedRedirectUrl.trim() || '/admin'; const targetUrl = isAdminOrIssuer ? adminRedirect : '/portfolio'; // Force a full page refresh to ensure new auth state is recognized window.location.replace(targetUrl); } catch (err) { const error = err as Error; form.setError('root', { message: `Unexpected error: ${error.message}`, }); } }, onError: (ctx) => { form.setError('root', { message: ctx.error.message, }); }, } ); }; return (
{ e.preventDefault(); form.handleSubmit(onSubmit)(e); }} {...props} >

Create an account

Enter the information below to create your account and managed blockchain wallet

{form.formState.errors.root && ( {form.formState.errors.root.message?.toString() || 'An error occurred'} )}
( Name )} /> ( Email )} /> ( Password )} /> ( Choose a secure wallet PIN code )} /> ( Confirm wallet PIN code )} />
Already have an account?{' '} Sign in
); }