Skip to content

The open alternative to Salesforce, designed for AI.

Keep default domain deep links through sign-in and workspace selection (#26073)

martmull4b0dbc34749a
diff --git a/packages/twenty-front/src/modules/app/components/RootAuthorizeRedirect.tsx b/packages/twenty-front/src/modules/app/components/RootAuthorizeRedirect.tsx
deleted file mode 100644
index 90895528658..00000000000
--- a/packages/twenty-front/src/modules/app/components/RootAuthorizeRedirect.tsx
+++ /dev/null
@@ -1,18 +0,0 @@
-import { createSearchParams, Navigate, useLocation } from 'react-router-dom';
-import { AppPath } from 'twenty-shared/types';
-
-export const RootAuthorizeRedirect = () => {
- const { hash, pathname, search } = useLocation();
-
- const returnToPath = `${pathname}${search}${hash}`;
-
- return (
- <Navigate
- to={{
- pathname: AppPath.SignInUp,
- search: createSearchParams({ returnToPath }).toString(),
- }}
- replace
- />
- );
-};
diff --git a/packages/twenty-front/src/modules/app/components/RootDeepLinkRedirect.tsx b/packages/twenty-front/src/modules/app/components/RootDeepLinkRedirect.tsx
new file mode 100644
index 00000000000..88790b17c14
--- /dev/null
+++ b/packages/twenty-front/src/modules/app/components/RootDeepLinkRedirect.tsx
@@ -0,0 +1,32 @@
+import { Navigate, useLocation } from 'react-router-dom';
+import { AppPath } from 'twenty-shared/types';
+import { isDefined } from 'twenty-shared/utils';
+
+import { isValidReturnToPath } from '@/auth/utils/isValidReturnToPath';
+
+export const RootDeepLinkRedirect = () => {
+ const { hash, pathname, search } = useLocation();
+
+ const searchParams = new URLSearchParams(search);
+ const returnToPathFromSearchParams = searchParams.get('returnToPath');
+
+ const returnToPath = isDefined(returnToPathFromSearchParams)
+ ? returnToPathFromSearchParams
+ : `${pathname}${search}${hash}`;
+
+ if (isValidReturnToPath(returnToPath)) {
+ searchParams.set('returnToPath', returnToPath);
+ } else {
+ searchParams.delete('returnToPath');
+ }
+
+ return (
+ <Navigate
+ to={{
+ pathname: AppPath.SignInUp,
+ search: searchParams.toString(),
+ }}
+ replace
+ />
+ );
+};
diff --git a/packages/twenty-front/src/modules/app/components/__tests__/RootDeepLinkRedirect.test.tsx b/packages/twenty-front/src/modules/app/components/__tests__/RootDeepLinkRedirect.test.tsx
new file mode 100644
index 00000000000..db2f48e70a0
--- /dev/null
+++ b/packages/twenty-front/src/modules/app/components/__tests__/RootDeepLinkRedirect.test.tsx
@@ -0,0 +1,80 @@
+import { render, screen } from '@testing-library/react';
+import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom';
+import { AppPath } from 'twenty-shared/types';
+
+import { RootDeepLinkRedirect } from '@/app/components/RootDeepLinkRedirect';
+
+const LocationProbe = () => {
+ const { pathname, search } = useLocation();
+
+ return <div data-testid="location">{`${pathname}${search}`}</div>;
+};
+
+const renderRedirect = (initialUrl: string) =>
+ render(
+ <MemoryRouter initialEntries={[initialUrl]}>
+ <Routes>
+ <Route path={AppPath.SignInUp} element={<LocationProbe />} />
+ <Route
+ path={AppPath.NotFoundWildcard}
+ element={<RootDeepLinkRedirect />}
+ />
+ </Routes>
+ </MemoryRouter>,
+ );
+
+const getLocation = () => screen.getByTestId('location').textContent;
+
+describe('RootDeepLinkRedirect', () => {
+ it('keeps a deep link as returnToPath', () => {
+ renderRedirect('/settings/applications/available/app-id');
+
+ expect(getLocation()).toBe(
+ '/welcome?returnToPath=%2Fsettings%2Fapplications%2Favailable%2Fapp-id',
+ );
+ });
+
+ it('keeps the deep link search params and hash inside returnToPath', () => {
+ renderRedirect('/settings/applications/available/app-id?tab=about#top');
+
+ expect(getLocation()).toBe(
+ '/welcome?tab=about&returnToPath=%2Fsettings%2Fapplications%2Favailable%2Fapp-id%3Ftab%3Dabout%23top',
+ );
+ });
+
+ it('does not nest an existing returnToPath', () => {
+ renderRedirect(
+ '/?returnToPath=%2Fsettings%2Fapplications%2Favailable%2Fapp-id',
+ );
+
+ expect(getLocation()).toBe(
+ '/welcome?returnToPath=%2Fsettings%2Fapplications%2Favailable%2Fapp-id',
+ );
+ });
+
+ it('keeps the authorize deep link as returnToPath', () => {
+ renderRedirect('/authorize?clientId=client-id');
+
+ expect(getLocation()).toBe(
+ '/welcome?clientId=client-id&returnToPath=%2Fauthorize%3FclientId%3Dclient-id',
+ );
+ });
+
+ it('adds no returnToPath for the index path', () => {
+ renderRedirect('/');
+
+ expect(getLocation()).toBe('/welcome');
+ });
+
+ it('adds no returnToPath for an authentication path', () => {
+ renderRedirect('/welcome/unknown');
+
+ expect(getLocation()).toBe('/welcome');
+ });
+
+ it('drops an unsafe returnToPath', () => {
+ renderRedirect('/?returnToPath=%2F%2Fevil.example.com');
+
+ expect(getLocation()).toBe('/welcome');
+ });
+});
diff --git a/packages/twenty-front/src/modules/app/hooks/useCreateRootAppRouter.tsx b/packages/twenty-front/src/modules/app/hooks/useCreateRootAppRouter.tsx
index 65dfe7545d1..72d81f922b2 100644
--- a/packages/twenty-front/src/modules/app/hooks/useCreateRootAppRouter.tsx
+++ b/packages/twenty-front/src/modules/app/hooks/useCreateRootAppRouter.tsx
@@ -2,14 +2,13 @@ import { lazy, useState } from 'react';
import {
createBrowserRouter,
createRoutesFromElements,
- Navigate,
Route,
} from 'react-router-dom';
import { AppPath } from 'twenty-shared/types';
import { LazyRoute } from '@/app/components/LazyRoute';
import { RootAppProviders } from '@/app/components/RootAppProviders';
-import { RootAuthorizeRedirect } from '@/app/components/RootAuthorizeRedirect';
+import { RootDeepLinkRedirect } from '@/app/components/RootDeepLinkRedirect';
import { VerifyEmail } from '@/auth/components/VerifyEmail';
import { OnboardingPageLoader } from '@/onboarding/components/OnboardingPageLoader';
import { OnboardingTransitionOutlet } from '@/onboarding/components/OnboardingTransitionOutlet';
@@ -63,10 +62,9 @@ const createRootAppRouter = () =>
}
/>
</Route>
- <Route path={AppPath.Authorize} element={<RootAuthorizeRedirect />} />
<Route
path={AppPath.NotFoundWildcard}
- element={<Navigate to={AppPath.SignInUp} replace />}
+ element={<RootDeepLinkRedirect />}
/>
</Route>,
),
diff --git a/packages/twenty-front/src/modules/settings/profile/components/DeleteWorkspace.tsx b/packages/twenty-front/src/modules/settings/profile/components/DeleteWorkspace.tsx
index 764f6cb1f77..ff7d537d2dd 100644
--- a/packages/twenty-front/src/modules/settings/profile/components/DeleteWorkspace.tsx
+++ b/packages/twenty-front/src/modules/settings/profile/components/DeleteWorkspace.tsx
@@ -10,6 +10,7 @@ import { IconTrash } from 'twenty-ui/icon';
import { H2Title } from 'twenty-ui/primitives/typography';
import { Button } from 'twenty-ui/primitives/input';
import { useMutation } from '@apollo/client/react';
+import { AppPath } from 'twenty-shared/types';
import { DeleteCurrentWorkspaceDocument } from '~/generated-metadata/graphql';
const DELETE_WORKSPACE_MODAL_ID = 'delete-workspace-modal';
@@ -27,7 +28,7 @@ export const DeleteWorkspace = () => {
const deleteWorkspace = async () => {
await deleteCurrentWorkspace();
await signOut();
- redirectToDefaultDomain();
+ redirectToDefaultDomain({ pathname: AppPath.SignInUp });
};
return (