fix(): sleep before redirect (#9079)

## Summary
This Pull Request centralizes the redirection logic by introducing a
reusable `useRedirect` hook, which replaces direct usage of
`window.location.href` with more standardized and testable functionality
across multiple modules.

- Introduced a new `useRedirect` hook for handling redirection logic
with optional controlled delays.
- Refactored redirection implementations in various modules (`useAuth`,
workspace, and settings-related hooks, etc.) to use the newly introduced
`useRedirect` or related high-level hooks.
- Updated API and documentation to include or improve support for SSO,
particularly OIDC and SAML setup processes in server logic.
- Enhanced frontend and backend configurability with new environment
variable settings for SSO.

---------

Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
Antoine Moreaux
2024-12-16 15:15:55 +01:00
committed by GitHub
parent 9e9c1bdff1
commit f8f3945680
11 changed files with 45 additions and 22 deletions

View File

@ -7,6 +7,7 @@ import {
MessageChannelVisibility,
useGenerateTransientTokenMutation,
} from '~/generated/graphql';
import { useRedirect } from '@/domain-manager/hooks/useRedirect';
const getProviderUrl = (provider: string) => {
switch (provider) {
@ -21,6 +22,7 @@ const getProviderUrl = (provider: string) => {
export const useTriggerApisOAuth = () => {
const [generateTransientToken] = useGenerateTransientTokenMutation();
const { redirect } = useRedirect();
const triggerApisOAuth = useCallback(
async (
@ -60,9 +62,9 @@ export const useTriggerApisOAuth = () => {
params += loginHint ? `&loginHint=${loginHint}` : '';
window.location.href = `${authServerUrl}/auth/${getProviderUrl(provider)}?${params}`;
redirect(`${authServerUrl}/auth/${getProviderUrl(provider)}?${params}`);
},
[generateTransientToken],
[generateTransientToken, redirect],
);
return { triggerApisOAuth };

View File

@ -6,10 +6,11 @@ import { useState } from 'react';
import { useRecoilState, useSetRecoilState } from 'recoil';
import { useImpersonateMutation } from '~/generated/graphql';
import { isDefined } from '~/utils/isDefined';
import { sleep } from '~/utils/sleep';
import { useRedirect } from '@/domain-manager/hooks/useRedirect';
export const useImpersonate = () => {
const { clearSession } = useAuth();
const { redirect } = useRedirect();
const [currentUser, setCurrentUser] = useRecoilState(currentUserState);
const setTokenPair = useSetRecoilState(tokenPairState);
const [impersonate] = useImpersonateMutation();
@ -43,8 +44,7 @@ export const useImpersonate = () => {
await clearSession();
setCurrentUser(user);
setTokenPair(tokens);
await sleep(0); // This hacky workaround is necessary to ensure the tokens stored in the cookie are updated correctly.
window.location.href = AppPath.Index;
redirect(AppPath.Index);
} catch (error) {
setError('Failed to impersonate user. Please try again.');
setIsLoading(false);