import React, { useState } from "react"; import { View, Text, StyleSheet, ScrollView, KeyboardAvoidingView, Platform, Alert, } from "react-native"; import { Link, router } from "expo-router"; import { SafeAreaView } from "react-native-safe-area-context"; import { useThemeColor } from "@/hooks/use-theme-color"; import { Button, Input } from "@/components/ui"; import { useAuth } from "@/context/AuthContext"; import { apiClient, User } from "@/lib/api"; import { registerForPushNotifications } from "@/hooks/useRegisterPushToken"; export default function LoginScreen() { const [email, setEmail] = useState(""); const [password, setPassword] = useState(""); const [errors, setErrors] = useState<{ email?: string; password?: string }>( {}, ); const { login, updateAuthUser, isLoading } = useAuth(); const textColor = useThemeColor({}, "text"); const backgroundColor = useThemeColor({}, "background"); const tintColor = useThemeColor({}, "tint"); const validateForm = () => { const newErrors: { email?: string; password?: string } = {}; if (!email) { newErrors.email = "Email é obrigatório"; } else if (!/\S+@\S+\.\S+/.test(email)) { newErrors.email = "Por favor, use um email válido"; } if (!password) { newErrors.password = "Senha é obrigatória"; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleLogin = async () => { if (!validateForm()) return; try { const response = await login(email, password); if (response?.user) { const token = await registerForPushNotifications(); if (token) { const updateResponse = await apiClient.updateUser({ id: response.user.id, pushToken: token, }); console.log("UPDATE USER response: ", updateResponse); if (updateResponse) { updateAuthUser(updateResponse.user); } } } router.replace("/"); } catch (error: any) { console.log("LOGIN error: ", error); Alert.alert("Erro: ", "Falha ao fazer login"); } }; return ( Bem vindo de volta Logue na sua conta nexo