diff --git a/src/view/screens/Login.tsx b/src/view/screens/Login.tsx index 02365288..ac93613e 100644 --- a/src/view/screens/Login.tsx +++ b/src/view/screens/Login.tsx @@ -112,17 +112,64 @@ const SigninOrCreateAccount = ({ const Signin = ({onPressBack}: {onPressBack: () => void}) => { const store = useStores() const [isProcessing, setIsProcessing] = useState(false) + const [serviceDescription, setServiceDescription] = useState< + ServiceDescription | undefined + >(undefined) const [error, setError] = useState('') const [handle, setHandle] = useState('') const [password, setPassword] = useState('') + useEffect(() => { + let aborted = false + if (serviceDescription || error) { + return + } + store.session.describeService(DEFAULT_SERVICE).then( + desc => { + if (aborted) return + setServiceDescription(desc) + }, + err => { + if (aborted) return + console.error(err) + setError( + 'Unable to contact your service. Please check your Internet connection.', + ) + }, + ) + return () => { + aborted = true + } + }, []) + const onPressNext = async () => { setError('') setIsProcessing(true) + + // try to guess the handle if the user just gave their own username try { + let fullHandle = handle + if ( + serviceDescription && + serviceDescription.availableUserDomains.length > 0 + ) { + let matched = false + for (const domain of serviceDescription.availableUserDomains) { + if (fullHandle.endsWith(domain)) { + matched = true + } + } + if (!matched) { + fullHandle = createFullHandle( + handle, + serviceDescription.availableUserDomains[0], + ) + } + } + await store.session.login({ service: DEFAULT_SERVICE, - handle, + handle: fullHandle, password, }) } catch (e: any) { @@ -161,10 +208,10 @@ const Signin = ({onPressBack}: {onPressBack: () => void}) => { ) : undefined} - +