Fix login with alternative domain

This commit is contained in:
Holger Huo 2024-12-27 00:40:17 +08:00
parent 8d2308704c
commit 0755230c82
No known key found for this signature in database
GPG key ID: BBD8E4260396640D
3 changed files with 42 additions and 18 deletions

View file

@ -60,3 +60,9 @@
color: var(--text-insignificant-color); color: var(--text-insignificant-color);
font-style: italic; font-style: italic;
} }
#settings input {
width: revert;
display: inline;
margin-top: 0.25em;
}

View file

@ -30,10 +30,12 @@ function Login() {
const [uiState, setUIState] = useState('default'); const [uiState, setUIState] = useState('default');
const [searchParams] = useSearchParams(); const [searchParams] = useSearchParams();
const instance = searchParams.get('instance'); const instance = searchParams.get('instance');
const isWebDomainParam = searchParams.get('is_web_domain');
const submit = searchParams.get('submit'); const submit = searchParams.get('submit');
const [instanceText, setInstanceText] = useState( const [instanceText, setInstanceText] = useState(
instance || cachedInstanceURL?.toLowerCase() || '', instance || cachedInstanceURL?.toLowerCase() || '',
); );
const [isWebDomain, setIsWebDomain] = useState(isWebDomainParam || false);
const [instancesList, setInstancesList] = useState([]); const [instancesList, setInstancesList] = useState([]);
const searcher = useRef(); const searcher = useRef();
@ -63,24 +65,28 @@ function Login() {
(async () => { (async () => {
// WEB_DOMAIN vs LOCAL_DOMAIN negotiation time // WEB_DOMAIN vs LOCAL_DOMAIN negotiation time
// https://docs.joinmastodon.org/admin/config/#web_domain // https://docs.joinmastodon.org/admin/config/#web_domain
try { if (!isWebDomain) {
const res = await fetch(`https://${instanceURL}/.well-known/host-meta`); // returns XML try {
const text = await res.text(); const res = await fetch(
// Parse XML `https://${instanceURL}/.well-known/host-meta`,
const parser = new DOMParser(); ); // returns XML
const xmlDoc = parser.parseFromString(text, 'text/xml'); const text = await res.text();
// Get Link[template] // Parse XML
const link = xmlDoc.getElementsByTagName('Link')[0]; const parser = new DOMParser();
const template = link.getAttribute('template'); const xmlDoc = parser.parseFromString(text, 'text/xml');
const url = URL.parse(template); // Get Link[template]
const { host } = url; // host includes the port const link = xmlDoc.getElementsByTagName('Link')[0];
if (instanceURL !== host) { const template = link.getAttribute('template');
console.log(`💫 ${instanceURL} -> ${host}`); const url = URL.parse(template);
instanceURL = host; const { host } = url; // host includes the port
if (instanceURL !== host) {
console.log(`💫 ${instanceURL} -> ${host}`);
instanceURL = host;
}
} catch (e) {
// Silently fail
console.error(e);
} }
} catch (e) {
// Silently fail
console.error(e);
} }
store.local.set('instanceURL', instanceURL); store.local.set('instanceURL', instanceURL);
@ -257,6 +263,18 @@ function Login() {
: t`Continue`} : t`Continue`}
</button>{' '} </button>{' '}
</div> </div>
<div id="settings">
<label>
<input
type="checkbox"
checked={isWebDomain}
onChange={(e) => {
setIsWebDomain(e.target.checked);
}}
/>{' '}
<Trans>This is the Web Domain used by Mastodon</Trans>
</label>
</div>
<Loader hidden={uiState !== 'loading'} /> <Loader hidden={uiState !== 'loading'} />
<hr /> <hr />
{!DEFAULT_INSTANCE && ( {!DEFAULT_INSTANCE && (

View file

@ -57,7 +57,7 @@ function Welcome() {
<Link <Link
to={ to={
DEFAULT_INSTANCE DEFAULT_INSTANCE
? `/login?instance=${DEFAULT_INSTANCE}&submit=1` ? `/login?instance=${DEFAULT_INSTANCE}&submit=1&is_web_domain=true`
: '/login' : '/login'
} }
class="button" class="button"