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);
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 [searchParams] = useSearchParams();
const instance = searchParams.get('instance');
const isWebDomainParam = searchParams.get('is_web_domain');
const submit = searchParams.get('submit');
const [instanceText, setInstanceText] = useState(
instance || cachedInstanceURL?.toLowerCase() || '',
);
const [isWebDomain, setIsWebDomain] = useState(isWebDomainParam || false);
const [instancesList, setInstancesList] = useState([]);
const searcher = useRef();
@ -63,24 +65,28 @@ function Login() {
(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;
if (!isWebDomain) {
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);
}
} catch (e) {
// Silently fail
console.error(e);
}
store.local.set('instanceURL', instanceURL);
@ -257,6 +263,18 @@ function Login() {
: t`Continue`}
</button>{' '}
</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'} />
<hr />
{!DEFAULT_INSTANCE && (

View file

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