fix(errors): fix error handling for signup/signin

This commit is contained in:
Waleed Latif
2025-05-19 01:25:58 -07:00
parent 8f75e795f2
commit b29827c4ee
3 changed files with 29 additions and 24 deletions

View File

@@ -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)

View File

@@ -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])

View File

@@ -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: [