'use client'; import React, { useState } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import Link from 'next/link'; import { Card, CardHeader, CardTitle, CardContent } from '@/components/ui/Card'; import { Input } from '@/components/ui/Input'; import { Button } from '@/components/ui/Button'; import { useTranslation } from '@/hooks/useTranslation'; import { useCsrf } from '@/hooks/useCsrf'; import { appendRedirectParam, sanitizeRedirectPath } from '@/lib/auth-flow'; export default function SignupClient() { const router = useRouter(); const searchParams = useSearchParams(); const { t } = useTranslation(); const { fetchWithCsrf } = useCsrf(); const [name, setName] = useState(''); const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [loading, setLoading] = useState(false); const [error, setError] = useState(''); const [showPassword, setShowPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const redirectTarget = sanitizeRedirectPath(searchParams.get('redirect')); const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setError(''); if (password !== confirmPassword) { setError('Passwords do not match'); setLoading(false); return; } if (password.length < 8) { setError('Password must be at least 8 characters'); setLoading(false); return; } try { const response = await fetchWithCsrf('/api/auth/signup', { method: 'POST', body: JSON.stringify({ name, email, password }), }); const data = await response.json(); if (response.ok && data.success) { // Store user in localStorage for client-side localStorage.setItem('user', JSON.stringify(data.user)); // Track successful signup with PostHog try { const { identifyUser, trackEvent } = await import('@/components/PostHogProvider'); identifyUser(data.user.id, { email: data.user.email, name: data.user.name, plan: data.user.plan || 'FREE', signupMethod: 'email', }); trackEvent('user_signup', { method: 'email', email: data.user.email, }); } catch (error) { console.error('PostHog tracking error:', error); } // Redirect to onboarding router.push(appendRedirectParam('/onboarding', redirectTarget)); router.refresh(); } else { setError(data.error || 'Failed to create account'); } } catch (err) { setError('An error occurred. Please try again.'); } finally { setLoading(false); } }; const handleGoogleSignIn = () => { // Redirect to Google OAuth API route window.location.href = appendRedirectParam('/api/auth/google', redirectTarget); }; return (
Start creating QR codes in seconds
← Back to HomeAlready have an account?{' '} Sign in
By signing up, you agree to our{' '} Privacy Policy