From df9628c1fd4b2335cfcb6215a9f134d5710cee25 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 8 Jan 2023 15:11:29 -0600 Subject: [PATCH] ExternalLoginForm: accept `?server` param to redirect the login form to the specified instance Fixes https://gitlab.com/soapbox-pub/soapbox/-/issues/1313 --- CHANGELOG.md | 1 + .../components/external-login-form.tsx | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 889eccdbf..a7a1288db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Compatibility: rudimentary support for Takahē. - UI: added backdrop blur behind modals. - Admin: let admins configure media preview for attachment thumbnails. +- Login: accept `?server` param in external login, eg `fe.soapbox.pub/login/external?server=gleasonator.com`. ### Changed - Posts: letterbox images to 19:6 again. diff --git a/app/soapbox/features/external-login/components/external-login-form.tsx b/app/soapbox/features/external-login/components/external-login-form.tsx index 18ec1da7a..570666679 100644 --- a/app/soapbox/features/external-login/components/external-login-form.tsx +++ b/app/soapbox/features/external-login/components/external-login-form.tsx @@ -17,12 +17,14 @@ const messages = defineMessages({ /** Form for logging into a remote instance */ const ExternalLoginForm: React.FC = () => { - const code = new URLSearchParams(window.location.search).get('code'); + const query = new URLSearchParams(window.location.search); + const code = query.get('code'); + const server = query.get('server'); const intl = useIntl(); const dispatch = useAppDispatch(); - const [host, setHost] = useState(''); + const [host, setHost] = useState(server || ''); const [isLoading, setLoading] = useState(false); const handleHostChange: React.ChangeEventHandler = ({ currentTarget }) => { @@ -44,6 +46,12 @@ const ExternalLoginForm: React.FC = () => { toast.error(intl.formatMessage(messages.networkFailed)); } + // If the server was invalid, clear it from the URL. + // https://stackoverflow.com/a/40592892 + if (server) { + window.history.pushState(null, '', window.location.pathname); + } + setLoading(false); }); }; @@ -54,7 +62,13 @@ const ExternalLoginForm: React.FC = () => { } }, [code]); - if (code) { + useEffect(() => { + if (server && !code) { + handleSubmit(); + } + }, [server]); + + if (code || server) { return ; }