import './login.css'; import { useEffect, useRef, useState } from 'preact/hooks'; import Link from '../components/link'; import Loader from '../components/loader'; import instancesListURL from '../data/instances.json?url'; import { getAuthorizationURL, registerApplication } from '../utils/auth'; import store from '../utils/store'; import useTitle from '../utils/useTitle'; function Login() { useTitle('Log in'); const instanceURLRef = useRef(); const cachedInstanceURL = store.local.get('instanceURL'); const [uiState, setUIState] = useState('default'); const [instancesList, setInstancesList] = useState([]); useEffect(() => { (async () => { try { const res = await fetch(instancesListURL); const data = await res.json(); setInstancesList(data); } catch (e) { // Silently fail console.error(e); } })(); }, []); useEffect(() => { if (cachedInstanceURL) { instanceURLRef.current.value = cachedInstanceURL.toLowerCase(); } }, []); const onSubmit = (e) => { e.preventDefault(); const { elements } = e.target; let instanceURL = elements.instanceURL.value.toLowerCase(); // Remove protocol from instance URL instanceURL = instanceURL.replace(/^https?:\/\//, '').replace(/\/+$/, ''); // Remove @acct@ or acct@ from instance URL instanceURL = instanceURL.replace(/^@?[^@]+@/, ''); store.local.set('instanceURL', instanceURL); (async () => { setUIState('loading'); try { const { client_id, client_secret } = await registerApplication({ instanceURL, }); if (client_id && client_secret) { store.session.set('clientID', client_id); store.session.set('clientSecret', client_secret); location.href = await getAuthorizationURL({ instanceURL, client_id, }); } else { alert('Failed to register application'); } setUIState('default'); } catch (e) { console.error(e); setUIState('error'); } })(); }; return (

Log in

{uiState === 'error' && (

Failed to log in. Please try again or another instance.

)}

{' '}

); } export default Login;