From 49c7ccb0cb0078ad07f4f0533f758cd2a7bddee9 Mon Sep 17 00:00:00 2001 From: Lim Chee Aun Date: Sat, 7 Sep 2024 17:08:30 +0800 Subject: [PATCH] Handle WEB_DOMAIN for login Turns out .well-known routes are CORS-enabled --- src/locales/en.po | 22 +++++++++++----------- src/pages/login.jsx | 25 ++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/locales/en.po b/src/locales/en.po index 9b9155f22..088c2d759 100644 --- a/src/locales/en.po +++ b/src/locales/en.po @@ -1345,7 +1345,7 @@ msgid "Accounts…" msgstr "" #: src/components/nav-menu.jsx:363 -#: src/pages/login.jsx:166 +#: src/pages/login.jsx:189 #: src/pages/status.jsx:792 #: src/pages/welcome.jsx:64 msgid "Log in" @@ -1740,7 +1740,7 @@ msgstr "" #: src/components/shortcuts-settings.jsx:75 #: src/components/shortcuts-settings.jsx:84 #: src/components/shortcuts-settings.jsx:122 -#: src/pages/login.jsx:170 +#: src/pages/login.jsx:193 msgid "Instance" msgstr "" @@ -2368,7 +2368,7 @@ msgstr "Login required." #: src/compose.jsx:90 #: src/pages/http-route.jsx:91 -#: src/pages/login.jsx:247 +#: src/pages/login.jsx:270 msgid "Go home" msgstr "" @@ -3052,32 +3052,32 @@ msgstr "" msgid "No lists yet." msgstr "" -#: src/pages/login.jsx:86 -#: src/pages/login.jsx:99 +#: src/pages/login.jsx:109 +#: src/pages/login.jsx:122 msgid "Failed to register application" msgstr "Failed to register application" -#: src/pages/login.jsx:185 +#: src/pages/login.jsx:208 msgid "instance domain" msgstr "instance domain" -#: src/pages/login.jsx:209 +#: src/pages/login.jsx:232 msgid "e.g. “mastodon.social”" msgstr "" -#: src/pages/login.jsx:220 +#: src/pages/login.jsx:243 msgid "Failed to log in. Please try again or try another instance." msgstr "" -#: src/pages/login.jsx:232 +#: src/pages/login.jsx:255 msgid "Continue with {selectedInstanceText}" msgstr "" -#: src/pages/login.jsx:233 +#: src/pages/login.jsx:256 msgid "Continue" msgstr "" -#: src/pages/login.jsx:241 +#: src/pages/login.jsx:264 msgid "Don't have an account? Create one!" msgstr "" diff --git a/src/pages/login.jsx b/src/pages/login.jsx index f0a85b42b..32b25e6f8 100644 --- a/src/pages/login.jsx +++ b/src/pages/login.jsx @@ -58,9 +58,32 @@ function Login() { const submitInstance = (instanceURL) => { if (!instanceURL) return; - store.local.set('instanceURL', instanceURL); (async () => { + // WEB_DOMAIN vs LOCAL_DOMAIN negotiation time + // https://docs.joinmastodon.org/admin/config/#web_domain + try { + const res = await fetch(`https://${instanceURL}/.well-known/host-meta`); // returns XML + const text = await res.text(); + // Parse XML + const parser = new DOMParser(); + const xmlDoc = parser.parseFromString(text, 'text/xml'); + // Get Link[template] + const link = xmlDoc.getElementsByTagName('Link')[0]; + const template = link.getAttribute('template'); + const url = URL.parse(template); + const { host } = url; // host includes the port + if (instanceURL !== host) { + console.log(`💫 ${instanceURL} -> ${host}`); + instanceURL = host; + } + } catch (e) { + // Silently fail + console.error(e); + } + + store.local.set('instanceURL', instanceURL); + setUIState('loading'); try { const { client_id, client_secret, vapid_key } =