import { useState } from "react"; import { useLocation } from "wouter"; import { useAuth } from "@/hooks/use-auth"; import { authApi } from "@/lib/api"; import logoUrl from "@assets/assets_task_01k3qp9hccec89tp5ht8ee88x5_1756363038_img_1-removebg-preview (1)_1756364677577.png"; import { useForm } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod"; import { z } from "zod"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { Checkbox } from "@/components/ui/checkbox"; import { useToast } from "@/hooks/use-toast"; import { Loader2 } from "lucide-react"; import { LocationDetector } from "@/components/location-detector"; const loginSchema = z.object({ email: z.string().email("Please enter a valid email"), password: z.string().min(6, "Password must be at least 6 characters"), }); const registerSchema = z.object({ username: z.string().min(3, "Username must be at least 3 characters"), email: z.string().email("Please enter a valid email"), password: z.string().min(6, "Password must be at least 6 characters"), confirmPassword: z.string().min(6, "Please confirm your password"), firstName: z.string().min(1, "First name is required"), lastName: z.string().min(1, "Last name is required"), phone: z.string().min(10, "Phone number is required"), // Location fields - manual entry (fallback) street: z.string().optional(), city: z.string().optional(), state: z.string().optional(), pinCode: z.string().optional(), country: z.string().optional(), // Automatic location fields latitude: z.number().optional(), longitude: z.number().optional(), googleMapsUrl: z.string().optional(), locationDetectedAutomatically: z.boolean().default(false), agreeToTerms: z.boolean().refine((val) => val === true, { message: "You must agree to the terms and conditions" }), }).refine((data) => data.password === data.confirmPassword, { message: "Passwords don't match", path: ["confirmPassword"], }); type LoginFormData = z.infer; type RegisterFormData = z.infer; export default function Auth() { const [, setLocation] = useLocation(); const { login } = useAuth(); const { toast } = useToast(); const [activeTab, setActiveTab] = useState("signin"); const [isLoading, setIsLoading] = useState(false); const [locationProvided, setLocationProvided] = useState(false); const [showLocationStep, setShowLocationStep] = useState(false); const loginForm = useForm({ resolver: zodResolver(loginSchema), defaultValues: { email: "", password: "", }, }); const registerForm = useForm({ resolver: zodResolver(registerSchema), defaultValues: { username: "", email: "", password: "", confirmPassword: "", firstName: "", lastName: "", phone: "", street: "", city: "", state: "", pinCode: "", country: "", latitude: undefined, longitude: undefined, googleMapsUrl: "", locationDetectedAutomatically: false, agreeToTerms: false, }, }); const onLogin = async (data: LoginFormData) => { setIsLoading(true); try { const response = await authApi.login(data); const result = await response.json(); login(result.token, result.user); toast({ title: "Welcome back!", description: "You have been successfully logged in.", }); setLocation("/"); } catch (error) { toast({ variant: "destructive", title: "Login failed", description: "Invalid email or password. Please try again.", }); } finally { setIsLoading(false); } }; const handleLocationDetected = (locationData: { latitude: number; longitude: number; googleMapsUrl: string; locationDetectedAutomatically: boolean; }) => { registerForm.setValue('latitude', locationData.latitude); registerForm.setValue('longitude', locationData.longitude); registerForm.setValue('googleMapsUrl', locationData.googleMapsUrl); registerForm.setValue('locationDetectedAutomatically', locationData.locationDetectedAutomatically); // Clear manual location fields when automatic detection is used if (locationData.locationDetectedAutomatically) { registerForm.setValue('street', ''); registerForm.setValue('city', ''); registerForm.setValue('state', ''); registerForm.setValue('pinCode', ''); registerForm.setValue('country', ''); } setLocationProvided(true); setShowLocationStep(false); }; const handleManualLocationSubmit = (manualData: { street: string; city: string; state: string; pinCode: string; country: string; locationDetectedAutomatically: false; }) => { registerForm.setValue('street', manualData.street); registerForm.setValue('city', manualData.city); registerForm.setValue('state', manualData.state); registerForm.setValue('pinCode', manualData.pinCode); registerForm.setValue('country', manualData.country); registerForm.setValue('locationDetectedAutomatically', false); // Clear automatic location fields when manual entry is used registerForm.setValue('latitude', undefined); registerForm.setValue('longitude', undefined); registerForm.setValue('googleMapsUrl', ''); setLocationProvided(true); setShowLocationStep(false); }; const onRegister = async (data: RegisterFormData) => { if (!locationProvided) { setShowLocationStep(true); toast({ title: "Location required", description: "Please provide your location to continue registration.", }); return; } setIsLoading(true); try { const { confirmPassword, agreeToTerms, ...registerData } = data; const response = await authApi.register(registerData); const result = await response.json(); login(result.token, result.user); toast({ title: "Account created!", description: "Welcome to Shoposphere! Your account has been created successfully.", }); setLocation("/"); } catch (error) { toast({ variant: "destructive", title: "Registration failed", description: "An error occurred while creating your account. Please try again.", }); } finally { setIsLoading(false); } }; return (
Shoposphere
Sign In Sign Up
( Email )} /> ( Password )} />
( First Name )} /> ( Last Name )} />
( Username )} /> ( Email )} /> ( Phone )} /> {/* Location Section */} {showLocationStep ? (
) : locationProvided ? (

✓ Location information provided

{registerForm.getValues('locationDetectedAutomatically') ? (

Coordinates: {registerForm.getValues('latitude')?.toFixed(6)}, {registerForm.getValues('longitude')?.toFixed(6)}

) : (

Manual address: {registerForm.getValues('street')}, {registerForm.getValues('city')}

)}
) : (

📍 Location information will be requested after you fill out the basic details above.

)} ( Password )} /> ( Confirm Password )} /> ( I agree to the Terms & Conditions )} />
); }