Skip to content

Commit

Permalink
Fix login with alternative domain
Browse files Browse the repository at this point in the history
  • Loading branch information
HolgerHuo committed Dec 26, 2024
1 parent 8d23087 commit ef44251
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
6 changes: 6 additions & 0 deletions src/pages/login.css
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@
color: var(--text-insignificant-color);
font-style: italic;
}

#settings input {
width: revert;
display: inline;
margin-top: 0.25em;
}
52 changes: 35 additions & 17 deletions src/pages/login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ 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();
Expand Down Expand Up @@ -63,24 +67,26 @@ 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);
Expand Down Expand Up @@ -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 && (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/welcome.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit ef44251

Please sign in to comment.