import { Link, useRouter } from "expo-router"; import { useCallback, useState } from "react"; import { Alert, Image, ScrollView, Text, View } from "react-native"; 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 SignIn = () => { const router = useRouter(); const { isLoaded, setSession } = useSession(); const [form, setForm] = useState({ email: "", password: "", }); const onSignInPress = useCallback(async () => { try { const response = await fetchAPI("/(api)/auth/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ email: form.email, password: form.password, }), }); await setSession(response.data); router.replace("/"); } catch (err: any) { const status = String(err?.message ?? ""); const message = status.includes("403") ? "Please verify your email first." : status.includes("401") ? "Invalid email or password." : "Could not sign in. Please try again."; Alert.alert("Error", message); setForm((prevForm) => ({ ...prevForm, password: "", })); } }, [isLoaded, form.email, form.password, setSession, router]); return ( Car Welcome 👋 setForm((prevForm) => ({ ...prevForm, email: value, })) } keyboardType="email-address" /> setForm((prevForm) => ({ ...prevForm, password: value, })) } /> Don't have an account? Sign up ); }; export default SignIn;