import React, { useState } from "react"; import { View, ScrollView, Pressable, TextInput, StyleSheet, ActivityIndicator, KeyboardAvoidingView, Platform, Image, } from "react-native"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Text } from "@/components/ui/text"; import { Button } from "@/components/ui/button"; import { Mail, Lock, ArrowRight, Eye, EyeOff, Chrome, User } from "@/lib/icons"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { useAuthStore } from "@/lib/auth-store"; import * as Linking from "expo-linking"; import { api, BASE_URL } from "@/lib/api"; import { useColorScheme } from "nativewind"; import { toast } from "@/lib/toast-store"; export default function LoginScreen() { const nav = useSirouRouter(); const setAuth = useAuthStore((state) => state.setAuth); const { colorScheme } = useColorScheme(); const isDark = colorScheme === "dark"; const [identifier, setIdentifier] = useState(""); const [password, setPassword] = useState(""); const [showPassword, setShowPassword] = useState(false); const [loading, setLoading] = useState(false); const handleLogin = async () => { if (!identifier || !password) { toast.error( "Required Fields", "Please enter both identifier and password", ); return; } setLoading(true); const isEmail = identifier.includes("@"); const payload = isEmail ? { email: identifier, password } : { phone: identifier, password }; try { // Using the new api.auth.login which is powered by simple-api const response = await api.auth.login({ body: payload }); // Store user, access token, and refresh token setAuth(response.user, response.accessToken, response.refreshToken); toast.success("Welcome Back!", "You have successfully logged in."); // Explicitly navigate to home nav.go("(tabs)"); } catch (err: any) { toast.error("Login Failed", err.message || "Invalid credentials"); } finally { setLoading(false); } }; const handleGoogleLogin = async () => { setLoading(true); try { // Hit api.auth.google directly — that's it const response = await api.auth.google(); setAuth(response.user, response.accessToken, response.refreshToken); toast.success("Welcome!", "Signed in with Google."); nav.go("(tabs)"); } catch (err: any) { console.error("[Login] Google Login Error:", err); toast.error( "Google Login Failed", err.message || "An unexpected error occurred.", ); } finally { setLoading(false); } }; return ( {/* Logo / Branding */} Login Sign in to manage your tickets & invoices {/* Form */} Email or Phone Number Password setShowPassword(!showPassword)}> {showPassword ? ( ) : ( )} {/* Social / Other */} or {loading ? ( ) : ( <> Continue with Google )} nav.go("register")} > Don't have an account?{" "} Create one ); }