import React, {useState, useEffect} from 'react' import { ActivityIndicator, TextInput, TouchableOpacity, View, } from 'react-native' import { FontAwesomeIcon, FontAwesomeIconStyle, } from '@fortawesome/react-native-fontawesome' import {ComAtprotoServerDescribeServer} from '@atproto/api' import * as EmailValidator from 'email-validator' import {BskyAgent} from '@atproto/api' import {useAnalytics} from 'lib/analytics/analytics' import {Text} from '../../util/text/Text' import {s} from 'lib/styles' import {toNiceDomain} from 'lib/strings/url-helpers' 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 {Trans, msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import {styles} from './styles' import {useModalControls} from '#/state/modals' type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema export const ForgotPasswordForm = ({ error, serviceUrl, serviceDescription, setError, setServiceUrl, onPressBack, onEmailSent, }: { error: string serviceUrl: string serviceDescription: ServiceDescription | undefined setError: (v: string) => void setServiceUrl: (v: string) => void onPressBack: () => void onEmailSent: () => void }) => { const pal = usePalette('default') const theme = useTheme() const [isProcessing, setIsProcessing] = useState(false) const [email, setEmail] = useState('') const {screen} = useAnalytics() const {_} = useLingui() const {openModal} = useModalControls() useEffect(() => { screen('Signin:ForgotPassword') }, [screen]) const onPressSelectService = () => { openModal({ name: 'server-input', initialService: serviceUrl, onSelect: setServiceUrl, }) } const onPressNext = async () => { if (!EmailValidator.validate(email)) { return setError(_(msg`Your email appears to be invalid.`)) } setError('') setIsProcessing(true) try { const agent = new BskyAgent({service: serviceUrl}) await agent.com.atproto.server.requestPasswordReset({email}) onEmailSent() } catch (e: any) { const errMsg = e.toString() logger.warn('Failed to request password reset', {error: e}) setIsProcessing(false) if (isNetworkError(e)) { setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) } else { setError(cleanError(errMsg)) } } } return ( <> Reset password Enter the email you used to create your account. We'll send you a "reset code" so you can set a new password. {toNiceDomain(serviceUrl)} {error ? ( {error} ) : undefined} Back {!serviceDescription || isProcessing ? ( ) : !email ? ( Next ) : ( Next )} {!serviceDescription || isProcessing ? ( Processing... ) : undefined} Already have a code? ) }