diff --git a/ui/lib/serverconfig.js b/ui/lib/serverconfig.js index 968173e3aa..cf32c97b61 100644 --- a/ui/lib/serverconfig.js +++ b/ui/lib/serverconfig.js @@ -2,7 +2,9 @@ import useSWR from 'swr' export function useServerConfig() { const { data: { isEmailConfigured, isSignupEnabled, baseDomain } = {} } = - useSWR(`/api/server-configuration`) + useSWR(`/api/server-configuration`, { + revalidateIfStale: false, + }) return { isEmailConfigured, diff --git a/ui/pages/login/callback.js b/ui/pages/login/callback.js index e6d0115f4a..9232c407f0 100644 --- a/ui/pages/login/callback.js +++ b/ui/pages/login/callback.js @@ -3,14 +3,12 @@ import { useRouter } from 'next/router' import { useSWRConfig } from 'swr' import { useUser } from '../../lib/hooks' -import { useServerConfig } from '../../lib/serverconfig' import { saveToVisitedOrgs } from '.' import LoginLayout from '../../components/layouts/login' export default function Callback() { const { mutate } = useSWRConfig() - const { baseDomain } = useServerConfig() const { login } = useUser() const router = useRouter() @@ -30,11 +28,7 @@ export default function Callback() { router.replace(next ? decodeURIComponent(next) : '/') window.localStorage.removeItem('next') - saveToVisitedOrgs( - window.location.host, - baseDomain, - user?.organizationName - ) + saveToVisitedOrgs(window.location.host, user?.organizationName) } const providerID = window.localStorage.getItem('providerID') @@ -45,8 +39,7 @@ export default function Callback() { state === window.localStorage.getItem('state') && code && providerID && - redirectURL && - baseDomain + redirectURL ) { finish({ providerID, @@ -58,7 +51,7 @@ export default function Callback() { window.localStorage.removeItem('state') window.localStorage.removeItem('redirectURL') } - }, [code, state, mutate, router, baseDomain, login]) + }, [code, state, mutate, router, login]) if (!isReady) { return null diff --git a/ui/pages/login/index.js b/ui/pages/login/index.js index 629d39d7b1..1d7aa46db9 100644 --- a/ui/pages/login/index.js +++ b/ui/pages/login/index.js @@ -29,7 +29,7 @@ function oidcLogin({ id, clientID, authURL, scopes }, next) { )}&state=${state}` } -export function saveToVisitedOrgs(domain, baseDomain, orgName) { +export function saveToVisitedOrgs(domain, orgName) { const cookies = new Cookies() let visitedOrgs = cookies.get('orgs') || [] @@ -40,9 +40,17 @@ export function saveToVisitedOrgs(domain, baseDomain, orgName) { name: orgName, }) + // set the cookie domain to a general base domain + let cookieDomain = window.location.host + let parts = cookieDomain.split('.') + if (parts.length > 2) { + parts.shift() // remove the org + cookieDomain = parts.join('.') // join the last two parts of the domain + } + cookies.set('orgs', visitedOrgs, { path: '/', - domain: `.${baseDomain}`, + domain: `.${cookieDomain}`, }) } } @@ -100,7 +108,7 @@ export default function Login() { const [password, setPassword] = useState('') const [error, setError] = useState('') const [errors, setErrors] = useState({}) - const { baseDomain, isEmailConfigured } = useServerConfig() + const { isEmailConfigured } = useServerConfig() const { login } = useUser() async function onSubmit(e) { @@ -125,11 +133,7 @@ export default function Login() { router.replace(next ? decodeURIComponent(next) : '/') - saveToVisitedOrgs( - window.location.host, - baseDomain, - data?.organizationName - ) + saveToVisitedOrgs(window.location.host, data?.organizationName) } catch (e) { console.error(e) if (e.fieldErrors) {