import React, {useState, useEffect} from 'react' import { ActivityIndicator, TextInput, TouchableOpacity, View, } from 'react-native' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {BskyAgent} from '@atproto/api' import {useAnalytics} from 'lib/analytics/analytics' import {Text} from '../../util/text/Text' import {s} from 'lib/styles' import {isNetworkError} from 'lib/strings/errors' import {usePalette} from 'lib/hooks/usePalette' import {useTheme} from 'lib/ThemeContext' import {cleanError} from 'lib/strings/errors' import {logger} from '#/logger' import {styles} from './styles' import {Trans, msg} from '@lingui/macro' import {useLingui} from '@lingui/react' export const SetNewPasswordForm = ({ error, serviceUrl, setError, onPressBack, onPasswordSet, }: { error: string serviceUrl: string setError: (v: string) => void onPressBack: () => void onPasswordSet: () => void }) => { const pal = usePalette('default') const theme = useTheme() const {screen} = useAnalytics() const {_} = useLingui() useEffect(() => { screen('Signin:SetNewPasswordForm') }, [screen]) const [isProcessing, setIsProcessing] = useState(false) const [resetCode, setResetCode] = useState('') const [password, setPassword] = useState('') const onPressNext = async () => { setError('') setIsProcessing(true) try { const agent = new BskyAgent({service: serviceUrl}) const token = resetCode.replace(/\s/g, '') await agent.com.atproto.server.resetPassword({ token, password, }) onPasswordSet() } catch (e: any) { const errMsg = e.toString() logger.warn('Failed to set new password', {error: e}) setIsProcessing(false) if (isNetworkError(e)) { setError( 'Unable to contact your service. Please check your Internet connection.', ) } else { setError(cleanError(errMsg)) } } } return ( <> Set new password You will receive an email with a "reset code." Enter that code here, then enter your new password. {error ? ( {error} ) : undefined} Back {isProcessing ? ( ) : !resetCode || !password ? ( Next ) : ( Next )} {isProcessing ? ( Updating... ) : undefined} ) }