import { router, useLocalSearchParams } from "expo-router"; import { useEffect, useState } from "react"; import { KeyboardAvoidingView, Platform, Pressable, ScrollView, StyleSheet, Text, View, } from "react-native"; import { FullScreen } from "@/components/Screen"; import { AuthBackground } from "@/components/AppBackground"; import { AuthServerPicker } from "@/components/AuthServerPicker"; import { HeadingText } from "@/components/Logo"; import { Button } from "@/components/ui/Button"; import { Card } from "@/components/ui/Card"; import { Input } from "@/components/ui/Input"; import { fonts, radii, spacing } from "@/constants/theme"; import { useAppTheme } from "@/contexts/ThemeContext"; import { resetPassword } from "@/lib/auth-api"; import type { ThemeColors } from "@/lib/theme-palette"; import { useThemedStyles } from "@/lib/use-themed-styles"; import { isRequiredString, isValidPassword } from "@/lib/form-validation"; export default function ResetPasswordScreen() { const styles = useThemedStyles(createResetPasswordStyles); const { token: tokenParam } = useLocalSearchParams<{ token?: string }>(); const [token, setToken] = useState(""); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [error, setError] = useState(null); const [success, setSuccess] = useState(false); const [loading, setLoading] = useState(false); const [serverReady, setServerReady] = useState(true); useEffect(() => { if (typeof tokenParam === "string" && tokenParam.length > 0) { setToken(tokenParam); } }, [tokenParam]); const tokenError = isRequiredString(token) ? undefined : "Reset token is required"; const passwordError = isValidPassword(password) ? undefined : password ? "Password must be at least 8 characters" : "Password is required"; const confirmError = confirmPassword && password !== confirmPassword ? "Passwords do not match" : undefined; const canSubmit = serverReady && isRequiredString(token) && isValidPassword(password) && password === confirmPassword && confirmPassword.length > 0; async function handleSubmit() { if (!canSubmit) return; setError(null); setLoading(true); try { await resetPassword(token.trim(), password); setSuccess(true); } catch (err) { setError(err instanceof Error ? err.message : "Reset failed"); } finally { setLoading(false); } } return ( router.back()}> ← Back Set new password Paste the reset token from your email, or open the link on this device. {success ? ( Password updated You can now sign in with your new password.