import React, { useState, useEffect } from 'react'; import { Text, Image, TouchableOpacity, KeyboardAvoidingView, Platform, ScrollView, ActivityIndicator, Alert, useWindowDimensions, } from 'react-native'; import { useRouter } from 'expo-router'; import { useColorScheme } from 'nativewind'; import { Lock, Eye, EyeOff, X, ContactRound } from 'lucide-react-native'; import Animated, { FadeInUp } from 'react-native-reanimated'; import AsyncStorage from '@react-native-async-storage/async-storage'; import { Container } from '~/src/components/Container'; import Input from '~/src/components/Input'; import { supabase } from '~/src/lib/supabase'; const ChangePasswordScreen = () => { const router = useRouter(); const { colorScheme } = useColorScheme(); const isDark = colorScheme === 'dark'; const { width } = useWindowDimensions(); const [newPassword, setNewPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [showNewPassword, setShowNewPassword] = useState(false); const [showConfirmPassword, setShowConfirmPassword] = useState(false); const [isLoading, setIsLoading] = useState(false); const [errors, setErrors] = useState<{ newPassword?: string; confirmPassword?: string }>({}); const validateForm = () => { const newErrors: { newPassword?: string; confirmPassword?: string } = {}; if (!newPassword) { newErrors.newPassword = 'New password is required'; } else if (newPassword.length < 8) { newErrors.newPassword = 'Password must be at least 8 characters'; } if (!confirmPassword) { newErrors.confirmPassword = 'Please confirm your password'; } else if (confirmPassword !== newPassword) { newErrors.confirmPassword = 'Passwords do not match'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleBack = async () => { const isResetting = await AsyncStorage.getItem('handlingPasswordReset'); if (isResetting) { Alert.alert( 'Warning', "You haven't changed your password yet. Are you sure you want to leave?", [ { text: 'Stay', style: 'cancel', }, { text: 'Leave', style: 'destructive', onPress: async () => { await AsyncStorage.removeItem('handlingPasswordReset'); router.back(); }, }, ] ); } else { router.back(); } }; const handleChangePassword = async () => { console.log('Starting handleChangePassword'); if (!validateForm()) { console.log('Form validation failed'); return; } console.log('Form validation passed'); setIsLoading(true); const result = await supabase.auth.updateUser({ password: newPassword }); console.log('updateUser result:', JSON.stringify(result, null, 2)); }; return ( {/* Close Button */} {/* MIT Logo */} {/* Change Password Illustration */} {/* Header Text */} Change Password Create a new secure password {/* New Password Input */} } label="New Password" placeholder="Enter new password" value={newPassword} onChangeText={(text) => { setNewPassword(text); setErrors({ ...errors, newPassword: undefined }); }} secureTextEntry={!showNewPassword} error={errors.newPassword} rightIcon={} onRightIconPress={() => setShowNewPassword(!showNewPassword)} helper="Password must be at least 8 characters" required /> {/* Confirm Password Input */} } placeholder="Confirm New Password" value={confirmPassword} onChangeText={(text) => { setConfirmPassword(text); setErrors({ ...errors, confirmPassword: undefined }); }} secureTextEntry={!showConfirmPassword} error={errors.confirmPassword} rightIcon={ setShowConfirmPassword(!showConfirmPassword)} className="p-1"> {showConfirmPassword ? ( ) : ( )} } /> {/* Submit Button */} {isLoading ? ( ) : ( Change Password )} {/* Cancel Button */} Cancel ); }; export default ChangePasswordScreen;