import { supabase } from "@/lib/supabase"; import { router } from "expo-router"; import { useEffect, useRef, useState } from "react"; import { Alert, Keyboard, KeyboardAvoidingView, KeyboardEvent, Platform, Pressable, ScrollView, Text, TextInput, TouchableWithoutFeedback, View } from "react-native"; export default function Login() { const [email, SetEmail] = useState(''); const [password, SetPassword] = useState(''); const [isLoading, SetIsLoading] = useState(false); const [isKeyboardVisible, setIsKeyboardVisible] = useState(false); const scrollViewRef = useRef(null); useEffect(() => { const showEvent = Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow'; const hideEvent = Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide'; const handleKeyboardShow = (event: KeyboardEvent) => { setIsKeyboardVisible(true); const keyboardHeight = event.endCoordinates.height; const offsetBaseline = Platform.OS === 'ios' ? 180 : 140; const nextScrollOffset = Math.max(0, keyboardHeight - offsetBaseline); scrollViewRef.current?.scrollTo({ y: nextScrollOffset, animated: true, }); }; const handleKeyboardHide = () => { setIsKeyboardVisible(false); scrollViewRef.current?.scrollTo({ y: 0, animated: true }); }; const showSubscription = Keyboard.addListener(showEvent, handleKeyboardShow); const hideSubscription = Keyboard.addListener(hideEvent, handleKeyboardHide); return () => { showSubscription.remove(); hideSubscription.remove(); }; }, []); const login = async () => { if(email.trim() === '' || password.trim() === '') { Alert.alert("All fields are required!"); return; } SetIsLoading(true); const { error } = await supabase.auth.signInWithPassword({ email, password }); SetIsLoading(false); if (error) { Alert.alert("Login failed, please check your credentials and try again"); return; } router.replace("/"); } const inputClassName = 'rounded-2xl border border-app-border bg-app-subtle px-4 py-3 text-base text-text-main' return ( Study Sprint Pick up where you left off. Log in Continue your study workflow. Your study path stays with your account Subjects, assignments, tasks, and tracked sprint progress follow you after you sign in. Email Password { scrollViewRef.current?.scrollToEnd({ animated: true }); }} /> {isLoading ? 'Logging in...' : 'Log in'} router.push('/createUser')} > Don't have an account? Sign up ); }