Skip to content

The open alternative to Salesforce, designed for AI.

fix(auth): prevent infinite redirect (#10065)

Antoine Moreauxaaea49d5f5cd
diff --git a/packages/twenty-front/src/hooks/useIsMatchingLocation.ts b/packages/twenty-front/src/hooks/useIsMatchingLocation.ts
index da23ae39b30..36f525f1fb0 100644
--- a/packages/twenty-front/src/hooks/useIsMatchingLocation.ts
+++ b/packages/twenty-front/src/hooks/useIsMatchingLocation.ts
@@ -3,28 +3,36 @@ import { matchPath, useLocation } from 'react-router-dom';
import { AppBasePath } from '@/types/AppBasePath';
import { isNonEmptyString } from '@sniptt/guards';
import { isDefined } from 'twenty-shared';
+import { useCallback } from 'react';
export const useIsMatchingLocation = () => {
const location = useLocation();
- const addTrailingSlash = (path: string) =>
- path.endsWith('/') ? path : path + '/';
-
- const getConstructedPath = (path: string, basePath?: AppBasePath) => {
- if (!isNonEmptyString(basePath)) return path;
-
- return addTrailingSlash(basePath) + path;
- };
-
- const isMatchingLocation = (path: string, basePath?: AppBasePath) => {
- const match = matchPath(
- getConstructedPath(path, basePath),
- location.pathname,
- );
- const isMatching = isDefined(match);
-
- return isMatching;
- };
+ // Infinite loop issue caused by `checkUserExistsQuery` in `useSignInUp`.
+ // Without executing this query, there is no infinite loop.
+ // I also noticed that in `isMatchingLocation` inside `continueWithEmail`, no loop occurs.
+ // Both functions are called within the `useEffect` of `SignInUpWorkspaceScopeFormEffect`.
+ // This led me to conclude that the issue comes from `useIsMatchingLocation`.
+ // Using `useCallback` prevent the loop.
+ const isMatchingLocation = useCallback(
+ (path: string, basePath?: AppBasePath) => {
+ const addTrailingSlash = (path: string) =>
+ path.endsWith('/') ? path : path + '/';
+
+ const getConstructedPath = (path: string, basePath?: AppBasePath) => {
+ if (!isNonEmptyString(basePath)) return path;
+
+ return addTrailingSlash(basePath) + path;
+ };
+
+ const match = matchPath(
+ getConstructedPath(path, basePath),
+ location.pathname,
+ );
+ return isDefined(match);
+ },
+ [location.pathname],
+ );
return {
isMatchingLocation,