From 5a5682f07148fed5d190bb2a29f1927cc2b1145d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 28 Apr 2025 17:56:53 -0400 Subject: [PATCH] Basic /authorize framework for cloud auth --- app/actions/definitions/teams.tsx | 4 +- app/components/Sidebar/App.tsx | 6 +- app/hooks/useRequest.ts | 2 +- .../{OrganizationMenu.tsx => TeamMenu.tsx} | 8 +- app/routes/authenticated.tsx | 3 - app/routes/index.tsx | 14 +-- app/scenes/Login/OAuthAuthorize.tsx | 67 +++++------- app/scenes/Login/components/ConnectHeader.tsx | 40 +++++++ app/scenes/Login/components/TeamSwitcher.tsx | 102 ++++++++++++++++++ shared/i18n/locales/en_US/translation.json | 2 + 10 files changed, 191 insertions(+), 57 deletions(-) rename app/menus/{OrganizationMenu.tsx => TeamMenu.tsx} (91%) create mode 100644 app/scenes/Login/components/ConnectHeader.tsx create mode 100644 app/scenes/Login/components/TeamSwitcher.tsx diff --git a/app/actions/definitions/teams.tsx b/app/actions/definitions/teams.tsx index a9847551d7..02be788fd6 100644 --- a/app/actions/definitions/teams.tsx +++ b/app/actions/definitions/teams.tsx @@ -11,7 +11,7 @@ import { ActionContext } from "~/types"; import Desktop from "~/utils/Desktop"; import { TeamSection } from "../sections"; -export const createTeamsList = ({ stores }: { stores: RootStore }) => +export const switchTeamsList = ({ stores }: { stores: RootStore }) => stores.auth.availableTeams?.map((session) => ({ id: `switch-${session.id}`, name: session.name, @@ -44,7 +44,7 @@ export const switchTeam = createAction({ section: TeamSection, visible: ({ stores }) => !!stores.auth.availableTeams && stores.auth.availableTeams?.length > 1, - children: createTeamsList, + children: switchTeamsList, }); export const createTeam = createAction({ diff --git a/app/components/Sidebar/App.tsx b/app/components/Sidebar/App.tsx index 1f89d2210c..633e3781fb 100644 --- a/app/components/Sidebar/App.tsx +++ b/app/components/Sidebar/App.tsx @@ -12,7 +12,7 @@ import useCurrentTeam from "~/hooks/useCurrentTeam"; import useCurrentUser from "~/hooks/useCurrentUser"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; -import OrganizationMenu from "~/menus/OrganizationMenu"; +import TeamMenu from "~/menus/TeamMenu"; import { homePath, searchPath } from "~/utils/routeHelpers"; import TeamLogo from "../TeamLogo"; import Tooltip from "../Tooltip"; @@ -62,7 +62,7 @@ function AppSidebar() { - + {(props: SidebarButtonProps) => ( )} - +
( if (makeRequestOnMount) { void request(); } - }, [request, makeRequestOnMount]); + }, []); return { data, loading, loaded, error, request }; } diff --git a/app/menus/OrganizationMenu.tsx b/app/menus/TeamMenu.tsx similarity index 91% rename from app/menus/OrganizationMenu.tsx rename to app/menus/TeamMenu.tsx index 1113476a71..6010ce7f7a 100644 --- a/app/menus/OrganizationMenu.tsx +++ b/app/menus/TeamMenu.tsx @@ -10,7 +10,7 @@ import { } from "~/actions/definitions/navigation"; import { createTeam, - createTeamsList, + switchTeamsList, desktopLoginTeam, } from "~/actions/definitions/teams"; import useActionContext from "~/hooks/useActionContext"; @@ -22,7 +22,7 @@ type Props = { children?: React.ReactNode; }; -const OrganizationMenu: React.FC = ({ children }: Props) => { +const TeamMenu: React.FC = ({ children }: Props) => { const menu = useMenuState({ unstable_offset: [4, -4], placement: "bottom-start", @@ -44,7 +44,7 @@ const OrganizationMenu: React.FC = ({ children }: Props) => { // menu is not cached at all. const actions = React.useMemo( () => [ - ...createTeamsList(context), + ...switchTeamsList(context), createTeam, desktopLoginTeam, separator(), @@ -64,4 +64,4 @@ const OrganizationMenu: React.FC = ({ children }: Props) => { ); }; -export default observer(OrganizationMenu); +export default observer(TeamMenu); diff --git a/app/routes/authenticated.tsx b/app/routes/authenticated.tsx index 0bfadfa57f..d778dc09f7 100644 --- a/app/routes/authenticated.tsx +++ b/app/routes/authenticated.tsx @@ -29,7 +29,6 @@ const Drafts = lazy(() => import("~/scenes/Drafts")); const Home = lazy(() => import("~/scenes/Home")); const Search = lazy(() => import("~/scenes/Search")); const Trash = lazy(() => import("~/scenes/Trash")); -const OAuthAuthorize = lazy(() => import("~/scenes/Login/OAuthAuthorize")); const RedirectDocument = ({ match, @@ -53,8 +52,6 @@ function AuthenticatedRoutes() { return ( - - import("~/components/Authenticated")); -const AuthenticatedRoutes = lazyWithRetry(() => import("./authenticated")); -const SharedDocument = lazyWithRetry(() => import("~/scenes/Document/Shared")); -const Login = lazyWithRetry(() => import("~/scenes/Login")); -const Logout = lazyWithRetry(() => import("~/scenes/Logout")); +const Authenticated = lazy(() => import("~/components/Authenticated")); +const AuthenticatedRoutes = lazy(() => import("./authenticated")); +const SharedDocument = lazy(() => import("~/scenes/Document/Shared")); +const Login = lazy(() => import("~/scenes/Login")); +const Logout = lazy(() => import("~/scenes/Logout")); +const OAuthAuthorize = lazy(() => import("~/scenes/Login/OAuthAuthorize")); export default function Routes() { useQueryNotices(); @@ -43,6 +44,7 @@ export default function Routes() { + diff --git a/app/scenes/Login/OAuthAuthorize.tsx b/app/scenes/Login/OAuthAuthorize.tsx index 03027fa8ec..9131b2aed9 100644 --- a/app/scenes/Login/OAuthAuthorize.tsx +++ b/app/scenes/Login/OAuthAuthorize.tsx @@ -1,23 +1,44 @@ -import { MoreIcon } from "outline-icons"; import React from "react"; import { Trans, useTranslation } from "react-i18next"; import styled from "styled-components"; import Flex from "@shared/components/Flex"; import { s } from "@shared/styles"; -import { Avatar, AvatarSize } from "~/components/Avatar"; -import { AvatarVariant } from "~/components/Avatar/Avatar"; +import { parseDomain } from "@shared/utils/domains"; import ButtonLarge from "~/components/ButtonLarge"; import Heading from "~/components/Heading"; import LoadingIndicator from "~/components/LoadingIndicator"; import PageTitle from "~/components/PageTitle"; import Text from "~/components/Text"; +import env from "~/env"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import useQuery from "~/hooks/useQuery"; import useRequest from "~/hooks/useRequest"; import { client } from "~/utils/ApiClient"; +import isCloudHosted from "~/utils/isCloudHosted"; +import Login from "./Login"; import { OAuthScopeHelper } from "./OAuthScopeHelper"; import { Background } from "./components/Background"; import { Centered } from "./components/Centered"; +import { ConnectHeader } from "./components/ConnectHeader"; +import { TeamSwitcher } from "./components/TeamSwitcher"; + +export default function OAuthAuthorize() { + const team = useCurrentTeam({ rejectOnEmpty: false }); + + // We're self-hosted or on a team subdomain already, just show the authorize screen. + if (team) { + return ; + } + + // Cloud hosted and on root domain – show the workspace switcher. + const isAppRoot = + parseDomain(window.location.hostname).host === parseDomain(env.URL).host; + if (isCloudHosted && isAppRoot) { + return ; + } + + return ; +} /** * Authorize component is responsible for handling the OAuth authorization process. @@ -40,17 +61,10 @@ function Authorize() { scope, } = Object.fromEntries(params); const [scopes] = React.useState(() => scope?.split(" ") ?? []); - const { - error: clientError, - data: response, - request, - } = useRequest(() => client.post("/oauthClients.info", { clientId })); - - React.useEffect(() => { - if (clientId) { - void request(); - } - }, []); + const { error: clientError, data: response } = useRequest( + () => client.post("/oauthClients.info", { clientId }), + true + ); const handleCancel = () => { if (window.history.length) { @@ -119,28 +133,7 @@ function Authorize() { - - - - - - - - - + {t(`{{ appName }} wants to access {{ teamName }}`, { appName: name, @@ -244,5 +237,3 @@ const Pre = styled.pre` font-size: 12px; white-space: pre-wrap; `; - -export default Authorize; diff --git a/app/scenes/Login/components/ConnectHeader.tsx b/app/scenes/Login/components/ConnectHeader.tsx new file mode 100644 index 0000000000..4e7bb6d97c --- /dev/null +++ b/app/scenes/Login/components/ConnectHeader.tsx @@ -0,0 +1,40 @@ +import { MoreIcon } from "outline-icons"; +import * as React from "react"; +import Flex from "@shared/components/Flex"; +import Text from "@shared/components/Text"; +import type Team from "~/models/Team"; +import type OAuthClient from "~/models/oauth/OAuthClient"; +import { Avatar } from "~/components/Avatar"; +import { AvatarSize, AvatarVariant } from "~/components/Avatar/Avatar"; + +type Props = { + team: Team; + oauthClient: OAuthClient; +}; + +export function ConnectHeader({ team, oauthClient }: Props) { + return ( + + + + + + + + + + ); +} diff --git a/app/scenes/Login/components/TeamSwitcher.tsx b/app/scenes/Login/components/TeamSwitcher.tsx new file mode 100644 index 0000000000..4f4e0c011c --- /dev/null +++ b/app/scenes/Login/components/TeamSwitcher.tsx @@ -0,0 +1,102 @@ +import { ArrowIcon } from "outline-icons"; +import React from "react"; +import { useTranslation } from "react-i18next"; +import styled from "styled-components"; +import { getCookie } from "tiny-cookie"; +import Text from "@shared/components/Text"; +import { s } from "@shared/styles"; +import { AvatarSize } from "~/components/Avatar"; +import Avatar, { AvatarVariant } from "~/components/Avatar/Avatar"; +import Heading from "~/components/Heading"; +import OutlineIcon from "~/components/Icons/OutlineIcon"; +import env from "~/env"; +import { Background } from "./Background"; +import { Centered } from "./Centered"; + +export function TeamSwitcher() { + const sessions = useSessions(); + const { t } = useTranslation(); + const url = new URL(window.location.href); + const appName = env.APP_NAME; + + return ( + + + + + {t("Choose a workspace")} + + {t( + "Choose an existing {{ appName }} workspace or login to continue connecting this app", + { appName } + )} + . + + {Object.keys(sessions)?.map((teamId) => { + const session = sessions[teamId]; + const location = session.url + url.pathname + url.search; + return ( + + + {session.name} + + + ); + })} + + + ); +} + +const StyledArrowIcon = styled(ArrowIcon)` + position: absolute; + transition: all 0.2s ease-in-out; + opacity: 0; + right: 12px; +`; + +const TeamLink = styled.a` + position: relative; + display: flex; + align-items: center; + gap: 8px; + padding: 8px; + margin: 0 -8px; + border-radius: 8px; + width: 100%; + border-bottom: 1px solid ${s("divider")}; + color: ${s("text")}; + font-weight: ${s("fontWeightMedium")}; + + &:last-child { + border-bottom: none; + } + + &:hover { + background: ${s("listItemHoverBackground")}; + + ${StyledArrowIcon} { + opacity: 1; + right: 8px; + } + } +`; + +const StyledHeading = styled(Heading).attrs({ + as: "h2", + centered: true, +})` + margin-top: 0; +`; + +function useSessions() { + return JSON.parse(getCookie("sessions") || "{}"); +} diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index 5576e578f2..3b5be58592 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -819,6 +819,8 @@ "Authentication failed – this login method was disabled by a workspace admin.": "Authentication failed – this login method was disabled by a workspace admin.", "The workspace you are trying to join requires an invite before you can create an account.<1>Please request an invite from your workspace admin and try again.": "The workspace you are trying to join requires an invite before you can create an account.<1>Please request an invite from your workspace admin and try again.", "Sorry, an unknown error occurred.": "Sorry, an unknown error occurred.", + "Choose a workspace": "Choose a workspace", + "Choose an existing {{ appName }} workspace or login to continue connecting this app": "Choose an existing {{ appName }} workspace or login to continue connecting this app", "Login": "Login", "Error": "Error", "Failed to load configuration.": "Failed to load configuration.",