import { Link, router } from "expo-router"; import { useState } from "react"; import { Alert, Image, ScrollView, Text, 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 { icons, images } from "@/constants"; import { fetchAPI } from "@/lib/fetch"; import { useSession } from "@/lib/session"; const SignUp = () => { const { setSession } = useSession(); const [form, setForm] = useState({ name: "", email: "", phone: "", password: "", }); const [verification, setVerification] = useState({ state: "default", error: "", code: "", }); 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 { 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, }), }); setVerification((prevVerification) => ({ ...prevVerification, state: "pending", })); setForm((prevForm) => ({ ...prevForm, password: "", })); } catch (err: any) { setForm((prevForm) => ({ ...prevForm, password: "", })); Alert.alert("Error", err?.message ?? "Could not create your account."); } }; const onPressVerify = async () => { try { const response = await fetchAPI("/(api)/auth/verify", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: form.email, code: verification.code }), }); await setSession(response.data); setVerification((prevVerification) => ({ ...prevVerification, state: "success", })); } catch (err: any) { setVerification((prevVerification) => ({ ...prevVerification, error: err?.message?.includes("400") ? "Invalid or expired verification code." : err?.message ?? "Verification failed.", state: "failed", })); } }; return ( Car Create Your Account 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: "success", })) } isVisible={verification.state === "pending"} > Verification We've sent a verification code to {form.email} setVerification((prevVerification) => ({ ...prevVerification, code, })) } /> {verification.error && ( {verification.error} )} Check Verified You've succesfully verified your account. router.push("/")} className="mt-5" /> ); }; export default SignUp;