Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382import {useRef, useState} from 'react'import {Keyboard, LayoutAnimation, Platform, View} from 'react-native'import {type ComAtprotoServerDescribeServer} from '@atproto/api'import {msg} from '@lingui/core/macro'import {useLingui} from '@lingui/react'import {Trans} from '@lingui/react/macro'
import {cleanError, isNetworkError} from '#/lib/strings/errors'import { buildHandleCandidates, isCorrectedLoginIdentifier, normalizeLoginIdentifier,} from '#/lib/strings/handles'import {logger} from '#/logger'import {useSessionApi} from '#/state/session'import {getOAuthClient, signInNativeAndroid} from '#/state/session/oauth-client'import { isHandleResolutionError, resolveBareNameToHandles, resolveDeactivatedHandle,} from '#/state/session/resolveForLogin'import {atoms as a, useTheme} from '#/alf'import {Button, ButtonIcon, ButtonText} from '#/components/Button'import {FormError} from '#/components/forms/FormError'import * as TextField from '#/components/forms/TextField'import {At_Stroke2_Corner0_Rounded as At} from '#/components/icons/At'import {Loader} from '#/components/Loader'import {Text} from '#/components/Typography'import {IS_E2E} from '#/env'import {FormContainer} from './FormContainer'
export const LoginForm = ({ error, serviceUrl, serviceDescription, initialHandle, setError, onPressBack,}: { error: string serviceUrl: string serviceDescription: ComAtprotoServerDescribeServer.OutputSchema | undefined initialHandle: string setError: (v: string) => void onPressBack: () => void}) => { const [isProcessing, setIsProcessing] = useState<boolean>(false) const [password, setPassword] = useState('') const [awaitingRedirect, setAwaitingRedirect] = useState(false) const abortRef = useRef<AbortController | null>(null) const processingRef = useRef(false) const [identifierValue, setIdentifierValue] = useState<string>( initialHandle || '', ) const [handleOptions, setHandleOptions] = useState<string[] | null>(null) const [showGuessConfirm, setShowGuessConfirm] = useState(false) const t = useTheme() const {_} = useLingui() const {login} = useSessionApi()
const onPressNext = async () => { if (isProcessing || processingRef.current) return processingRef.current = true try { Keyboard.dismiss() LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut) setError('') setHandleOptions(null) setShowGuessConfirm(false)
const identifier = normalizeLoginIdentifier(identifierValue)
if (!identifier) { setError(_(msg`Please enter your username or handle`)) return }
if (isCorrectedLoginIdentifier(identifierValue, identifier)) { setIdentifierValue(identifier) setShowGuessConfirm(true) return }
if (!identifier.startsWith('did:') && !identifier.includes('.')) { if (!serviceDescription) { setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) return } setIsProcessing(true) try { const matches = await resolveBareNameToHandles( identifier, serviceDescription.availableUserDomains, ) if (matches.length === 0) { const examples = buildHandleCandidates( identifier, serviceDescription.availableUserDomains, ) .slice(0, 2) .join(' or ') setError( _( msg`We couldn't find an account for "${identifier}". Enter your full handle including its domain (like ${examples})`, ), ) return } if (matches.length > 1) { setHandleOptions(matches) return } setIdentifierValue(matches[0]) setShowGuessConfirm(true) return } catch (e) { logger.warn('Failed to resolve bare name during sign-in', { error: String(e), }) setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) return } finally { setIsProcessing(false) } }
setIdentifierValue(identifier) await signInWithIdentifier(identifier) } finally { processingRef.current = false } }
const onPressHandleOption = (handle: string) => { if (isProcessing) return setHandleOptions(null) setError('') setIdentifierValue(handle) setShowGuessConfirm(true) }
const signInWithIdentifier = async (identifier: string) => { if (IS_E2E) { if (!password) { setError(_(msg`Please enter your password`)) return } setIsProcessing(true) try { await login({service: serviceUrl, identifier, password}, 'LoginForm') } catch (e) { const errMsg = String(e) if (isNetworkError(e)) { setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) } else { setError(cleanError(errMsg)) } } finally { setIsProcessing(false) } return }
const client = getOAuthClient() const controller = Platform.OS === 'android' ? new AbortController() : null if (controller) { abortRef.current = controller setAwaitingRedirect(true) } /* eslint-disable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access -- Expo OAuth types do not resolve in Linux CI */ const doSignIn = (id: string) => controller ? signInNativeAndroid(client, id, {signal: controller.signal}) : client.signIn(id)
try { let session try { session = await doSignIn(identifier) } catch (e) { if (!isHandleResolutionError(e)) throw e if (controller?.signal.aborted) throw new Error('OAUTH_CANCELLED') const did = await resolveDeactivatedHandle(identifier) if (controller?.signal.aborted) throw new Error('OAUTH_CANCELLED') session = await doSignIn(did) } /* eslint-enable @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access */
// On native, signInNativeAndroid/signIn returns the session directly. // On web, the browser redirects away and App.web.tsx handles the callback. if (Platform.OS !== 'web' && session) { await login( {service: '', identifier: '', password: '', oauthSession: session}, 'LoginForm', ) } } catch (e) { const errMsg = String(e) LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut) if (errMsg.includes('OAUTH_CANCELLED')) { // User-initiated cancel (Android): silent reset, no error banner. } else if (isNetworkError(e)) { logger.warn('Failed to start OAuth sign-in due to network error', { error: errMsg, }) setError( _( msg`Unable to contact your service. Please check your Internet connection.`, ), ) } else if (isHandleResolutionError(e)) { logger.warn('Failed to resolve handle during sign-in', { error: errMsg, }) setError( _( msg`We couldn't find an account with the handle "${identifier}". Check the spelling, or create a new account if you don't have one yet.`, ), ) } else { logger.warn('Failed to start OAuth sign-in', {error: errMsg}) setError(cleanError(errMsg)) } } finally { setIsProcessing(false) setAwaitingRedirect(false) abortRef.current = null } }
return ( <FormContainer testID="loginForm" titleText={<Trans>Sign in</Trans>}> <View> <TextField.LabelText> <Trans>Account</Trans> </TextField.LabelText> <View style={[a.gap_sm]}> <TextField.Root> <TextField.Icon icon={At} /> <TextField.Input testID="loginUsernameInput" label={_(msg`Username or handle`)} autoCapitalize="none" autoFocus autoCorrect={false} autoComplete="username" returnKeyType="done" textContentType="username" value={identifierValue} onChangeText={v => { setIdentifierValue(v) if (handleOptions) { setHandleOptions(null) } if (showGuessConfirm) { setShowGuessConfirm(false) } }} onSubmitEditing={() => void onPressNext()} editable={!isProcessing} accessibilityHint={_( msg`Enter your handle (e.g. alice.bsky.social)`, )} /> </TextField.Root> </View> </View> <FormError error={error} /> {IS_E2E && ( <View> <TextField.LabelText> <Trans>Password</Trans> </TextField.LabelText> <TextField.Root> <TextField.Input testID="loginPasswordInput" label={_(msg`Password`)} autoCapitalize="none" autoCorrect={false} autoComplete="password" secureTextEntry value={password} onChangeText={setPassword} onSubmitEditing={() => void onPressNext()} editable={!isProcessing} /> </TextField.Root> </View> )} {handleOptions && ( <View style={[a.gap_xs]}> <Text style={[a.text_sm, t.atoms.text_contrast_medium]}> <Trans> We found more than one account with that name. Which one is yours? </Trans> </Text> {handleOptions.map(handle => ( <Button key={handle} testID={`handleOption-${handle}`} label={_(msg`Sign in as ${handle}`)} variant="outline" color="primary" size="large" onPress={() => onPressHandleOption(handle)}> <ButtonText>{handle}</ButtonText> </Button> ))} </View> )} {showGuessConfirm && ( <Text style={[a.text_sm, t.atoms.text_contrast_medium]}> <Trans> We filled in our best guess at your full handle. If it looks right, press Login to continue </Trans> </Text> )} {awaitingRedirect && ( <Text style={[a.text_sm, a.text_center, t.atoms.text_contrast_medium]}> <Trans> Finishing sign-in… complete it in your browser, or cancel. </Trans> </Text> )} <View style={[a.flex_row, a.align_center, a.pt_md]}> <Button label={_(msg`Back`)} variant="solid" color="secondary" size="large" onPress={onPressBack}> <ButtonText> <Trans>Back</Trans> </ButtonText> </Button> <View style={a.flex_1} /> {awaitingRedirect ? ( <Button testID="loginCancelButton" label={_(msg`Cancel`)} accessibilityHint={_(msg`Cancels the sign-in process`)} variant="solid" color="secondary" size="large" onPress={() => abortRef.current?.abort()}> <ButtonText> <Trans>Cancel</Trans> </ButtonText> </Button> ) : ( <Button testID="loginNextButton" label={_(msg`Login`)} accessibilityHint={_(msg`Starts the sign-in process`)} variant="solid" color="primary" size="large" onPress={() => void onPressNext()}> <ButtonText> <Trans>Login</Trans> </ButtonText> {isProcessing && <ButtonIcon icon={Loader} />} </Button> )} </View> </FormContainer> )}