diff --git a/app/routes/index.tsx b/app/routes/index.tsx index 6a9a30f285..2e3e5ddbcd 100644 --- a/app/routes/index.tsx +++ b/app/routes/index.tsx @@ -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 ( - - - + {showTeamSwitcher ? ( + } /> + ) : ( + + + + )} )} diff --git a/app/scenes/Login/OAuthAuthorize.tsx b/app/scenes/Login/OAuthAuthorize.tsx index c8fa3720a5..85db74919d 100644 --- a/app/scenes/Login/OAuthAuthorize.tsx +++ b/app/scenes/Login/OAuthAuthorize.tsx @@ -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 ; + if (isCloudHosted && hasRememberedWorkspaces && isAppRootDomain()) { + return ( + + ); } return ; diff --git a/app/scenes/Login/components/TeamSwitcher.tsx b/app/scenes/Login/components/TeamSwitcher.tsx index ae2befd017..1a30bfdbc3 100644 --- a/app/scenes/Login/components/TeamSwitcher.tsx +++ b/app/scenes/Login/components/TeamSwitcher.tsx @@ -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 setShowLogin(false)} />; @@ -45,17 +49,17 @@ export function TeamSwitcher({ sessions }: Props) { {t("Choose a workspace")} - {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, + })} . - {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 ( - + { 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 () => { diff --git a/server/utils/authentication.ts b/server/utils/authentication.ts index c167e696d2..4dc61244bf 100644 --- a/server/utils/authentication.ts +++ b/server/utils/authentication.ts @@ -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(); diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index c84682b0a9..0e98697fab 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -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.": "Already have an account? Go to <1>login.", + "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",