import React, { useState } from "react"; import { useGoogleLogin } from "@react-oauth/google"; import { api } from "../../lib/api"; const PhoneInputScreen = ({ onRequestOtp, onCancel, onGoogleSuccess, onRegisterSuccess }) => { const [activeTab, setActiveTab] = useState("login"); // Register Form State const [name, setName] = useState(""); const [email, setEmail] = useState(""); const [phone, setPhone] = useState(""); const [password, setPassword] = useState(""); const [isRegistering, setIsRegistering] = useState(false); const [errorMsg, setErrorMsg] = useState(""); const login = useGoogleLogin({ onSuccess: onGoogleSuccess, onError: () => alert("Login Failed") }); const handleRegister = async () => { if (!name || !email || !phone || !password) { setErrorMsg("Please fill all fields."); return; } setIsRegistering(true); setErrorMsg(""); try { // Connects cleanly with the api configuration defined by VITE_API_URL or defaulted to local proxy. const response = await api.post("/api/auth/register", { name, email, phone, password, }); // Pass the successful name/email down the line dynamically onRegisterSuccess({ name: response.user?.name || name, email: response.user?.email || email, phone: response.user?.phone || phone }); } catch (err) { if (err.message.includes("409") || err.message.toLowerCase().includes("conflict") || err.message.toLowerCase().includes("already registered")) { setErrorMsg("Email already registered. Did you mean to log in?"); } else { setErrorMsg(err.message || "Registration failed. Please try again."); } } finally { setIsRegistering(false); } }; return (
{/* Left Promo Column */}
Steering wheel

10% OFF
on your first booking!

Use the coupon below to get flat 10% OFF on your first booking with road.rentals.

RE1ST10
{/* Right Auth Column */}
{/* Tab Switcher */}
{activeTab === "login" ? ( <>

Welcome Back

Sign in to book your rides, manage trips, and enjoy a seamless travel experience

{/* Phone Input Box */}
🇮🇳
{/* Buttons Row */}
) : ( <>

Create an Account

Register now to start your reliable travel journey with road.rentals

{/* Register Fields */}
setName(e.target.value)} className="w-full rounded-xl border border-gray-300 focus:border-primary-600 focus:ring-1 focus:ring-primary-600 outline-none px-4 py-3.5 text-sm transition-all" /> setEmail(e.target.value)} className="w-full rounded-xl border border-gray-300 focus:border-primary-600 focus:ring-1 focus:ring-primary-600 outline-none px-4 py-3.5 text-sm transition-all" />
🇮🇳
setPhone(e.target.value)} className="flex-1 w-full outline-none px-4 py-3.5 text-sm font-semibold rounded-r-xl placeholder:font-normal" />
setPassword(e.target.value)} className="w-full rounded-xl border border-gray-300 focus:border-primary-600 focus:ring-1 focus:ring-primary-600 outline-none px-4 py-3.5 text-sm transition-all" /> {errorMsg && (
{errorMsg}
)}
{/* Buttons Row */}
)} {/* Divider */}
or
{/* Google Btn */}
); }; export default PhoneInputScreen;