From b29827c4eedac10ac7b838224c6d45607e4c3b67 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Mon, 19 May 2025 01:25:58 -0700 Subject: [PATCH] fix(errors): fix error handling for signup/signin --- apps/sim/app/(auth)/signup/signup-form.tsx | 24 ++++++++----------- .../sim/app/(auth)/verify/use-verification.ts | 22 ++++++++++++----- apps/sim/lib/auth.ts | 7 +++--- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/apps/sim/app/(auth)/signup/signup-form.tsx b/apps/sim/app/(auth)/signup/signup-form.tsx index c113b190a9..d6a9b060b2 100644 --- a/apps/sim/app/(auth)/signup/signup-form.tsx +++ b/apps/sim/app/(auth)/signup/signup-form.tsx @@ -257,26 +257,22 @@ function SignupFormContent({ return } - // Send verification OTP in Prod try { await client.emailOtp.sendVerificationOtp({ email: emailValue, type: 'email-verification', }) - - if (typeof window !== 'undefined') { - sessionStorage.setItem('verificationEmail', emailValue) - localStorage.setItem('has_logged_in_before', 'true') - document.cookie = 'has_logged_in_before=true; path=/; max-age=31536000; SameSite=Lax' // 1 year expiry - } - - router.push('/verify') - } catch (error) { - console.error('Failed to send verification code:', error) - setPasswordErrors(['Account created but failed to send verification code.']) - setShowValidationError(true) - router.push('/login') + } catch (err) { + console.error('Failed to send verification OTP:', err) } + + if (typeof window !== 'undefined') { + sessionStorage.setItem('verificationEmail', emailValue) + localStorage.setItem('has_logged_in_before', 'true') + document.cookie = 'has_logged_in_before=true; path=/; max-age=31536000; SameSite=Lax' // 1 year expiry + } + + router.push('/verify?fromSignup=true') } catch (error) { console.error('Signup error:', error) setIsLoading(false) diff --git a/apps/sim/app/(auth)/verify/use-verification.ts b/apps/sim/app/(auth)/verify/use-verification.ts index ce9beb7df0..661a780ed3 100644 --- a/apps/sim/app/(auth)/verify/use-verification.ts +++ b/apps/sim/app/(auth)/verify/use-verification.ts @@ -212,13 +212,23 @@ export function useVerification({ } useEffect(() => { - if (!isProduction || !hasResendKey) { - setIsVerified(true) - const timeoutId = setTimeout(() => { - router.push('/w') - }, 1000) + if (typeof window !== 'undefined') { + if (!isProduction || !hasResendKey) { + const storedEmail = sessionStorage.getItem('verificationEmail') + logger.info('Auto-verifying user', { email: storedEmail }) + } - return () => clearTimeout(timeoutId) + const isDevOrDocker = !isProduction || process.env.DOCKER_BUILD === 'true' + + // Auto-verify and redirect in development/docker environments + if (isDevOrDocker || !hasResendKey) { + setIsVerified(true) + const timeoutId = setTimeout(() => { + router.push('/w') + }, 1000) + + return () => clearTimeout(timeoutId) + } } }, [isProduction, hasResendKey, router]) diff --git a/apps/sim/lib/auth.ts b/apps/sim/lib/auth.ts index 1fb8a4c13d..1502ed63e4 100644 --- a/apps/sim/lib/auth.ts +++ b/apps/sim/lib/auth.ts @@ -21,7 +21,6 @@ import { env } from './env' const logger = createLogger('Auth') const isProd = env.NODE_ENV === 'production' -const isDevOrDocker = env.NODE_ENV === 'development' || env.DOCKER_BUILD // Only initialize Stripe if the key is provided // This allows local development without a Stripe account @@ -205,9 +204,9 @@ export const auth = betterAuth({ throw error } }, - sendVerificationOnSignUp: isProd, - otpLength: 6, - expiresIn: 15 * 60, + sendVerificationOnSignUp: false, + otpLength: 6, // Explicitly set the OTP length + expiresIn: 15 * 60, // 15 minutes in seconds }), genericOAuth({ config: [