import React, { useState } from "react"; import { KeyboardAvoidingView, Platform, ScrollView, StyleSheet, View, Alert, } from "react-native"; import { SafeAreaView } from "react-native-safe-area-context"; import { router } from "expo-router"; import { moderateScale, verticalScale } from "react-native-size-matters"; import SettingsHeader from "@/features/settings/components/SettingsHeader"; import PasswordInput from "@/components/forms/PasswordInput"; import PrimaryButton from "@/components/buttons/PrimaryButton"; import FieldLabel from "@/components/forms/FieldLabel"; import { Colors } from "@/theme"; import { apiClient } from "@/api"; export default function ChangePasswordScreen() { const [currentPassword, setCurrentPassword] = useState(""); const [newPassword, setNewPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [currentError, setCurrentError] = useState(""); const [newError, setNewError] = useState(""); const [confirmError, setConfirmError] = useState(""); const [isLoading, setIsLoading] = useState(false); const handleUpdatePassword = async () => { let hasError = false; setCurrentError(""); setNewError(""); setConfirmError(""); if (!currentPassword) { setCurrentError("Current password is required"); hasError = true; } if (!newPassword) { setNewError("New password is required"); hasError = true; } else if (newPassword.length < 8) { setNewError("New password must be at least 8 characters"); hasError = true; } else if (/\s/.test(newPassword)) { setNewError("Password must not contain spaces"); hasError = true; } else if (!/[A-Z]/.test(newPassword)) { setNewError("Must contain at least one uppercase letter"); hasError = true; } else if (!/[0-9]/.test(newPassword)) { setNewError("Must contain at least one number"); hasError = true; } else if (!/[^A-Za-z0-9\s]/.test(newPassword)) { setNewError("Must contain at least one special character"); hasError = true; } if (!confirmPassword) { setConfirmError("Please confirm your new password"); hasError = true; } else if (newPassword !== confirmPassword) { setConfirmError("Passwords do not match"); hasError = true; } if (hasError) return; setIsLoading(true); try { await apiClient.post("/auth/change-password", { old_password: currentPassword, new_password: newPassword, }); const msg = "Password updated successfully!"; if (Platform.OS === "web") { window.alert(msg); router.back(); } else { Alert.alert("Success", msg, [ { text: "OK", onPress: () => router.back() }, ]); } } catch (err: any) { const errMsg = err?.response?.data?.detail || "Failed to update password. Please check your current password."; setCurrentError(errMsg); if (Platform.OS === "web") { window.alert(errMsg); } else { Alert.alert("Error", errMsg); } } finally { setIsLoading(false); } }; return ( ); } const styles = StyleSheet.create({ safeArea: { flex: 1, backgroundColor: Colors.background, }, keyboardContainer: { flex: 1, }, contentContainer: { flexGrow: 1, paddingBottom: verticalScale(32), }, container: { flex: 1, paddingHorizontal: moderateScale(20), paddingTop: verticalScale(12), }, form: { marginTop: verticalScale(8), }, field: { marginBottom: verticalScale(16), }, buttonSpacing: { marginTop: verticalScale(12), }, });