import React, {useState, useRef} from 'react' import { ActivityIndicator, Keyboard, TextInput, TouchableOpacity, View, } from 'react-native' import {ComAtprotoServerDescribeServer} from '@atproto/api' import {Trans, msg} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useAnalytics} from 'lib/analytics/analytics' import {createFullHandle} from 'lib/strings/handles' import {isNetworkError} from 'lib/strings/errors' import {useSessionApi} from '#/state/session' import {cleanError} from 'lib/strings/errors' import {logger} from '#/logger' import {Button, ButtonText} from '#/components/Button' import {atoms as a, useTheme} from '#/alf' import {Text} from '#/components/Typography' import * as TextField from '#/components/forms/TextField' import {At_Stroke2_Corner0_Rounded as At} from '#/components/icons/At' import {Lock_Stroke2_Corner0_Rounded as Lock} from '#/components/icons/Lock' import {HostingProvider} from '#/components/forms/HostingProvider' import {FormContainer} from './FormContainer' import {FormError} from '#/components/forms/FormError' type ServiceDescription = ComAtprotoServerDescribeServer.OutputSchema export const LoginForm = ({ error, serviceUrl, serviceDescription, initialHandle, setError, setServiceUrl, onPressRetryConnect, onPressBack, onPressForgotPassword, }: { error: string serviceUrl: string serviceDescription: ServiceDescription | undefined initialHandle: string setError: (v: string) => void setServiceUrl: (v: string) => void onPressRetryConnect: () => void onPressBack: () => void onPressForgotPassword: () => void }) => { const {track} = useAnalytics() const t = useTheme() const [isProcessing, setIsProcessing] = useState(false) const [identifier, setIdentifier] = useState(initialHandle) const [password, setPassword] = useState('') const passwordInputRef = useRef(null) const {_} = useLingui() const {login} = useSessionApi() const onPressSelectService = React.useCallback(() => { Keyboard.dismiss() track('Signin:PressedSelectService') }, [track]) const onPressNext = async () => { Keyboard.dismiss() setError('') setIsProcessing(true) try { // try to guess the handle if the user just gave their own username let fullIdent = identifier if ( !identifier.includes('@') && // not an email !identifier.includes('.') && // not a domain serviceDescription && serviceDescription.availableUserDomains.length > 0 ) { let matched = false for (const domain of serviceDescription.availableUserDomains) { if (fullIdent.endsWith(domain)) { matched = true } } if (!matched) { fullIdent = createFullHandle( identifier, serviceDescription.availableUserDomains[0], ) } } // TODO remove double login await login({ service: serviceUrl, identifier: fullIdent, password, }) } catch (e: any) { const errMsg = e.toString() setIsProcessing(false) if (errMsg.includes('Authentication Required')) { logger.debug('Failed to login due to invalid credentials', { error: errMsg, }) setError(_(msg`Invalid username or password`)) } else if (isNetworkError(e)) { logger.warn('Failed to login due to network error', {error: errMsg}) setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) } else { logger.warn('Failed to login', {error: errMsg}) setError(cleanError(errMsg)) } } } const isReady = !!serviceDescription && !!identifier && !!password return ( Sign in}> Hosting provider Account { passwordInputRef.current?.focus() }} blurOnSubmit={false} // prevents flickering due to onSubmitEditing going to next field value={identifier} onChangeText={str => setIdentifier((str || '').toLowerCase().trim()) } editable={!isProcessing} accessibilityHint={_( msg`Input the username or email address you used at signup`, )} /> Forgot? {!serviceDescription && error ? ( ) : !serviceDescription ? ( <> Connecting... ) : isProcessing ? ( ) : isReady ? ( ) : undefined} ) }