diff --git a/app/components/Authenticated.tsx b/app/components/Authenticated.tsx index e0fc321b3..ce0ab787f 100644 --- a/app/components/Authenticated.tsx +++ b/app/components/Authenticated.tsx @@ -2,9 +2,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { Redirect } from "react-router-dom"; -import { parseDomain } from "@shared/utils/domains"; import LoadingIndicator from "~/components/LoadingIndicator"; -import env from "~/env"; import useStores from "~/hooks/useStores"; import { changeLanguage } from "~/utils/language"; @@ -25,27 +23,11 @@ const Authenticated = ({ children }: Props) => { if (auth.authenticated) { const { user, team } = auth; - const { hostname } = window.location; if (!team || !user) { return ; } - // If we're authenticated but viewing a domain that doesn't match the - // current team then kick the user to the teams correct domain. - if (team.domain) { - if (team.domain !== hostname) { - window.location.href = `${team.url}${window.location.pathname}`; - return ; - } - } else if ( - env.SUBDOMAINS_ENABLED && - parseDomain(hostname).teamSubdomain !== (team.subdomain ?? "") - ) { - window.location.href = `${team.url}${window.location.pathname}`; - return ; - } - return children; } diff --git a/app/stores/AuthStore.ts b/app/stores/AuthStore.ts index 1e4873a84..8676d9840 100644 --- a/app/stores/AuthStore.ts +++ b/app/stores/AuthStore.ts @@ -2,7 +2,7 @@ import * as Sentry from "@sentry/react"; import invariant from "invariant"; import { observable, action, computed, autorun, runInAction } from "mobx"; import { getCookie, setCookie, removeCookie } from "tiny-cookie"; -import { getCookieDomain } from "@shared/utils/domains"; +import { getCookieDomain, parseDomain } from "@shared/utils/domains"; import RootStore from "~/stores/RootStore"; import Policy from "~/models/Policy"; import Team from "~/models/Team"; @@ -162,6 +162,23 @@ export default class AuthStore { }); } + // Redirect to the correct custom domain or team subdomain if needed + // Occurs when the (sub)domain is changed in admin and the user hits an old url + const { hostname, pathname } = window.location; + + if (this.team.domain) { + if (this.team.domain !== hostname) { + window.location.href = `${team.url}${pathname}`; + return; + } + } else if ( + env.SUBDOMAINS_ENABLED && + parseDomain(hostname).teamSubdomain !== (team.subdomain ?? "") + ) { + window.location.href = `${team.url}${pathname}`; + return; + } + // If we came from a redirect then send the user immediately there const postLoginRedirectPath = getCookie("postLoginRedirectPath");