import { Link, router } from "expo-router"; import { useCallback, useState } from "react"; import { Alert, Image, KeyboardAvoidingView, Platform, ScrollView, Text, TouchableOpacity, View, } from "react-native"; import ReactNativeModal from "react-native-modal"; import { CustomButton } from "@/components/custom-button"; import { InputField } from "@/components/input-field"; import { OAuth } from "@/components/oauth"; import { OtpField } from "@/components/otp-field"; import { icons, images } from "@/constants"; import { ApiError, fetchAPI } from "@/lib/fetch"; import { useSession } from "@/lib/session"; const ROLES = [ { value: "rider", title: "I need a ride", description: "Book rides and get where you're going", }, { value: "driver", title: "I want to drive", description: "Offer rides and earn money with your car", }, ] as const; type Role = (typeof ROLES)[number]["value"]; const SignUp = () => { const { setSession } = useSession(); const [role, setRole] = useState("rider"); const [form, setForm] = useState({ name: "", email: "", phone: "", password: "", }); // "verified" is a hand-off state: it hides the code modal so its onModalHide // can bring up the success modal, since two modals can't cross-fade. const [verification, setVerification] = useState({ state: "default" as "default" | "pending" | "verified" | "success", error: "", code: "", devCode: "", busy: false, }); const onSignUpPress = async () => { if (!form.name.trim() || !form.email.trim() || !form.password) { Alert.alert( "Missing information", "Please fill in your name, email and password.", ); return; } if (form.phone.trim() && !/^[0-9\s\-()+.]+$/.test(form.phone)) { Alert.alert( "Invalid phone number", "Enter a valid Lebanese number, e.g. 70 123 456.", ); return; } try { const response = await fetchAPI("/(api)/auth/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: form.name, email: form.email, phone: form.phone.trim(), password: form.password, role, }), }); setVerification({ state: "pending", error: "", code: "", busy: false, devCode: (response as { data?: { devCode?: string } })?.data?.devCode ?? "", }); setForm((prevForm) => ({ ...prevForm, password: "", })); } catch (err: any) { setForm((prevForm) => ({ ...prevForm, password: "", })); Alert.alert( "Error", err instanceof ApiError && err.status < 500 ? err.message : "Could not create your account.", ); } }; const onPressVerify = useCallback( async (code: string) => { if (!/^\d{6}$/.test(code)) { setVerification((prevVerification) => ({ ...prevVerification, error: "Enter the 6-digit code.", })); return; } setVerification((prevVerification) => // Guard the double submit that auto-verify + a button tap would cause. prevVerification.busy ? prevVerification : { ...prevVerification, busy: true, error: "" }, ); try { const response = await fetchAPI("/(api)/auth/verify", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: form.email, code }), }); await setSession(response.data); setVerification((prevVerification) => ({ ...prevVerification, state: "verified", busy: false, })); } catch (err: any) { // Stay on the code modal so the user can retry; only a real success // advances the flow. setVerification((prevVerification) => ({ ...prevVerification, code: "", busy: false, error: err instanceof ApiError && err.status < 500 ? err.message : "Verification failed. Please try again.", })); } }, [form.email, setSession], ); return ( Car Create Your Account How will you use Waseel? {ROLES.map((option) => { const selected = role === option.value; return ( setRole(option.value)} activeOpacity={0.8} className={`flex-1 justify-center rounded-2xl border p-4 ${ selected ? "border-primary-500 bg-primary-500/10" : "border-neutral-100 bg-neutral-100" }`} > {`${option.title} {option.title} {option.description} ); })} setForm((prevForm) => ({ ...prevForm, name: value, })) } autoCapitalize="words" /> setForm((prevForm) => ({ ...prevForm, email: value, })) } keyboardType="email-address" /> setForm((prevForm) => ({ ...prevForm, phone: value, })) } keyboardType="phone-pad" /> setForm((prevForm) => ({ ...prevForm, password: value, })) } /> Already have an account? Sign in setVerification((prevVerification) => prevVerification.state === "verified" ? { ...prevVerification, state: "success" } : prevVerification, ) } isVisible={verification.state === "pending"} > Verification We've sent a verification code to {form.email} {verification.devCode ? ( Email delivery is not configured on this server. Your verification code is{" "} {verification.devCode} ) : null} setVerification((prevVerification) => ({ ...prevVerification, code, error: "", })) } onComplete={onPressVerify} /> {verification.error ? ( {verification.error} ) : null} onPressVerify(verification.code)} disabled={verification.busy} className="mt-5 bg-emerald-500" /> Check Verified You've succesfully verified your account. router.push("/")} className="mt-5" /> ); }; export default SignUp;