feat: Show workspace chooser on root domain for multiple sessions (#14013)

* feat: Show workspace chooser on root domain when signed in to several workspaces

* chore: Update translation strings
This commit is contained in:
Tom Moor authored and GitHub committed 2026-11-02 20:34:02 -05:00
1 parent 6d7cd455e9
commit 133fba4dd0
7 files changed
+73 -31

No files matched your search

+21 -3
View File
@@ -9,6 +9,9 @@ import env from "~/env";
import lazy from "@shared/utils/lazyWithRetry";
import { matchDocumentSlug as documentSlug } from "~/utils/routeHelpers";
import useAutoRefresh from "~/hooks/useAutoRefresh";
import { useWorkspaceSessions } from "~/hooks/useWorkspaceSessions";
import isCloudHosted from "~/utils/isCloudHosted";
import { isAppRootDomain } from "~/utils/urls";
const Authenticated = lazy(() => import("~/components/Authenticated"));
const AuthenticatedRoutes = lazy(() => import("./authenticated"));
@@ -16,6 +19,11 @@ const Shared = lazy(() => import("~/scenes/Shared"));
const Login = lazy(() => import("~/scenes/Login"));
const Logout = lazy(() => import("~/scenes/Logout"));
const OAuthAuthorize = lazy(() => import("~/scenes/Login/OAuthAuthorize"));
const TeamSwitcher = lazy(() =>
import("~/scenes/Login/components/TeamSwitcher").then((module) => ({
default: module.TeamSwitcher,
}))
);
/**
* Loads the chunks that a share page renders, so that the first render of the
@@ -29,6 +37,12 @@ export function preloadShared() {
export default function Routes() {
useAutoRefresh();
const sessions = useWorkspaceSessions();
// The root domain of a cloud hosted install has no workspace session, so
// app paths list the signed-in workspaces to continue to.
const showTeamSwitcher =
isCloudHosted && isAppRootDomain() && Object.keys(sessions).length > 0;
return (
<Suspense
@@ -71,9 +85,13 @@ export default function Routes() {
component={Shared}
/>
<Authenticated>
<AuthenticatedRoutes />
</Authenticated>
{showTeamSwitcher ? (
<Route render={() => <TeamSwitcher sessions={sessions} />} />
) : (
<Authenticated>
<AuthenticatedRoutes />
</Authenticated>
)}
</Switch>
)}
</Suspense>
+12 -5
View File
@@ -3,8 +3,8 @@ import { Trans, useTranslation } from "react-i18next";
import styled from "styled-components";
import Flex from "@shared/components/Flex";
import { s } from "@shared/styles";
import { parseDomain } from "@shared/utils/domains";
import { getHostname, isLoopbackUri } from "@shared/utils/urls";
import { isAppRootDomain } from "~/utils/urls";
import { OAuthClientValidation } from "@shared/validations";
import type OAuthClient from "~/models/oauth/OAuthClient";
import ButtonLarge from "~/components/ButtonLarge";
@@ -36,6 +36,7 @@ import { TeamSwitcher } from "./components/TeamSwitcher";
import { Form } from "~/components/primitives/Form";
export default function OAuthAuthorize() {
const { t } = useTranslation();
const team = useCurrentTeam({ rejectOnEmpty: false });
const sessions = useWorkspaceSessions();
@@ -45,11 +46,17 @@ export default function OAuthAuthorize() {
}
// Cloud hosted and on root domain – show the workspace switcher.
const isAppRoot =
parseDomain(window.location.hostname).host === parseDomain(env.URL).host;
const hasRememberedWorkspaces = Object.keys(sessions).length > 0;
if (isCloudHosted && hasRememberedWorkspaces && isAppRoot) {
return <TeamSwitcher sessions={sessions} />;
if (isCloudHosted && hasRememberedWorkspaces && isAppRootDomain()) {
return (
<TeamSwitcher
sessions={sessions}
description={t(
"Choose an {{ appName }} workspace or log in to continue connecting this app",
{ appName: env.APP_NAME }
)}
/>
);
}
return <Login />;
+15 -11
View File
@@ -6,6 +6,7 @@ import Text from "@shared/components/Text";
import { s } from "@shared/styles";
import type { Sessions } from "@shared/utils/sessions";
import { isSessionActive } from "@shared/utils/sessions";
import { sanitizeUrl } from "@shared/utils/urls";
import { AvatarSize } from "~/components/Avatar";
import Avatar, { AvatarVariant } from "~/components/Avatar/Avatar";
import ChangeLanguage from "~/components/ChangeLanguage";
@@ -18,20 +19,23 @@ import { Background } from "./Background";
import { Centered } from "./Centered";
interface Props {
/** The remembered workspaces, including logged-out ones. */
sessions: Sessions;
/** Text shown below the heading, defaults to a generic prompt. */
description?: string;
}
/**
* Shows remembered workspaces with a login path for logged-out sessions.
* Shows remembered workspaces so the user can choose which one to continue to
* with the current path, with a login path for logged-out sessions.
*
* @param props the remembered workspace sessions.
* @param props the remembered workspace sessions and optional description.
* @returns the workspace selector.
*/
export function TeamSwitcher({ sessions }: Props) {
export function TeamSwitcher({ sessions, description }: Props) {
const { t } = useTranslation();
const [showLogin, setShowLogin] = useState(false);
const url = new URL(window.location.href);
const appName = env.APP_NAME;
if (showLogin) {
return <Login onBack={() => setShowLogin(false)} />;
@@ -45,17 +49,17 @@ export function TeamSwitcher({ sessions }: Props) {
<StyledHeading>{t("Choose a workspace")}</StyledHeading>
<Text type="tertiary" as="p">
{t(
"Choose an {{ appName }} workspace or log in to continue connecting this app",
{ appName }
)}
{description ??
t("Choose an {{ appName }} workspace or log in to continue", {
appName: env.APP_NAME,
})}
.
</Text>
{Object.keys(sessions)?.map((teamId) => {
{Object.keys(sessions).map((teamId) => {
const session = sessions[teamId];
const location = session.url + url.pathname + url.search;
const location = sanitizeUrl(session.url + url.pathname + url.search);
return (
<TeamLink href={location} key={session.url}>
<TeamLink href={location} key={teamId}>
<Avatar
variant={AvatarVariant.Square}
model={{
+12
View File
@@ -30,6 +30,18 @@ export function getRedirectUrl(authUrl: string) {
return url.toString();
}
/**
* Checks whether the app is loaded on the root application domain, rather
* than on a workspace subdomain or custom domain.
*
* @returns true if the current host is the host of env.URL.
*/
export function isAppRootDomain() {
return (
parseDomain(window.location.hostname).host === parseDomain(env.URL).host
);
}
/**
* Checks whether a link is a hash link to an anchor on the current page.
*
+2 -5
View File
@@ -666,7 +666,7 @@ describe("root domain session redirects", () => {
expect(res.headers.get("location")).toEqual(`${team.url}/settings/billing`);
});
it("should redirect to the first workspace when signed in to several", async () => {
it("should not redirect when signed in to several workspaces", async () => {
const first = await buildTeam({ subdomain: `first-${randomUUID()}` });
const second = await buildTeam({ subdomain: `second-${randomUUID()}` });
@@ -675,10 +675,7 @@ describe("root domain session redirects", () => {
redirect: "manual",
});
expect(res.status).toEqual(302);
expect(res.headers.get("location")).toEqual(
`${first.url}/settings/billing`
);
expect(res.status).toEqual(200);
});
it("should not redirect public paths", async () => {
+9 -6
View File
@@ -16,9 +16,10 @@ import { SessionsCookieHelper } from "./SessionsCookieHelper";
import { clearCSRFCookie } from "./csrf";
/**
* Finds the workspace to send a request for the app root domain to, using the
* first usable workspace in the "sessions" cookie. Entries that are malformed,
* expired, deleted, suspended, or already the current host are ignored.
* Finds the workspace to send a request for the app root domain to, when the
* "sessions" cookie has exactly one usable workspace. Entries that are
* malformed, expired, deleted, suspended, or already the current host are
* ignored.
*
* @param ctx the Koa context.
* @param teams the reconciled workspaces in cookie order.
@@ -29,14 +30,16 @@ export function getSessionRedirectUrl(
teams: Team[]
): string | undefined {
const activeSessions = getActiveSessions(SessionsCookieHelper.read(ctx));
const team = teams.find(
const candidates = teams.filter(
(team) => activeSessions[team.id] && !team.isTeamUrl(ctx.href)
);
if (!team) {
// With several usable workspaces the client shows a workspace chooser.
if (candidates.length !== 1) {
return;
}
const url = new URL(team.url);
const url = new URL(candidates[0].url);
url.pathname = ctx.path;
url.search = ctx.search;
return url.toString();
+2 -1
View File
@@ -1149,7 +1149,7 @@
"Enter the address of an Outline workspace to connect": "Enter the address of an Outline workspace to connect",
"This doesn’t appear to be a valid installation": "This doesn’t appear to be a valid installation",
"Choose a workspace": "Choose a workspace",
"Choose an {{ appName }} workspace or log in to continue connecting this app": "Choose an {{ appName }} workspace or log in to continue connecting this app",
"Choose an {{ appName }} workspace or log in to continue": "Choose an {{ appName }} workspace or log in to continue",
"Create workspace": "Create workspace",
"Set up your workspace by providing a name and details for admin login. You can change these later.": "Set up your workspace by providing a name and details for admin login. You can change these later.",
"Workspace name": "Workspace name",
@@ -1172,6 +1172,7 @@
"You signed in with {{ authProviderName }} last time.": "You signed in with {{ authProviderName }} last time.",
"Or": "Or",
"Already have an account? Go to <1>login</1>.": "Already have an account? Go to <1>login</1>.",
"Choose an {{ appName }} workspace or log in to continue connecting this app": "Choose an {{ appName }} workspace or log in to continue connecting this app",
"An error occurred": "An error occurred",
"The OAuth client could not be found, please check the provided client ID": "The OAuth client could not be found, please check the provided client ID",
"The OAuth client could not be loaded, please check the redirect URI is valid": "The OAuth client could not be loaded, please check the redirect URI is valid",