Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(): sleep before redirect #9079

Merged
merged 6 commits into from
Dec 16, 2024
Merged

fix(): sleep before redirect #9079

merged 6 commits into from
Dec 16, 2024

Conversation

AMoreaux
Copy link
Contributor

@AMoreaux AMoreaux commented Dec 16, 2024

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.

@AMoreaux AMoreaux self-assigned this Dec 16, 2024
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR introduces a sleep delay before redirects to ensure proper cookie and state updates across the application's navigation flows.

  • Added new useRedirect hook in /packages/twenty-front/src/modules/domain-manager/hooks/useRedirect.ts that enforces a sleep(0) delay before redirects
  • Modified /packages/twenty-front/src/modules/auth/hooks/useAuth.ts to use the new redirect hook for SSO and authentication flows
  • Added sleep(0) in /packages/twenty-front/src/modules/domain-manager/hooks/useGetPublicWorkspaceDataBySubdomain.ts error handler before default domain redirect
  • Replaced direct window.location.href assignments with useRedirect hook across multiple components for consistent redirect handling
  • Removed unused User repository dependency from /packages/twenty-server/src/engine/core-modules/sso/services/sso.service.ts

11 file(s) reviewed, 9 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 39 to 41
setLastAuthenticateWorkspaceDomain(null);
await sleep(0);
redirectToDefaultDomain();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding error details to console.error and documenting why sleep(0) is necessary here

import { sleep } from '~/utils/sleep';

export const useRedirect = () => {
const redirect = (url: string) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: missing URL validation - could allow redirect to malicious domains

Comment on lines +1 to +2
// Don't use this hook directly! Prefer the high level hooks like:
// useRedirectToDefaultDomain and useRedirectToWorkspaceDomain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: if this hook shouldn't be used directly, consider making it internal or private to prevent misuse


export const useRedirectToDefaultDomain = () => {
const { defaultDomain } = useReadDefaultDomainFromConfiguration();
const { redirect } = useRedirect();
const redirectToDefaultDomain = () => {
const url = new URL(window.location.href);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: URL constructor may throw if window.location.href is malformed. Consider wrapping in try/catch.

Comment on lines 9 to 12
if (url.hostname !== defaultDomain) {
url.hostname = defaultDomain;
window.location.href = url.toString();
redirect(url.toString());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: consider adding error handling in case redirect() fails or throws


export const useRedirectToWorkspaceDomain = () => {
const isMultiWorkspaceEnabled = useRecoilValue(isMultiWorkspaceEnabledState);
const { buildWorkspaceUrl } = useBuildWorkspaceUrl();
const { redirect } = useRedirect();

const redirectToWorkspaceDomain = (
subdomain: string,
pathname?: string,
searchParams?: Record<string, string>,
) => {
if (!isMultiWorkspaceEnabled) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: No return value when isMultiWorkspaceEnabled is false could cause unexpected behavior in calling code

Comment on lines 93 to 95
onCompleted: (data) => {
window.location.href = data.authorizeApp.redirectUrl;
redirect(data.authorizeApp.redirectUrl);
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: no error handling for failed authorization - should catch and handle errors from authorizeApp mutation

Comment on lines 44 to +47
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: setCurrentUser and setTokenPair state updates may not be reflected before the redirect occurs. Consider awaiting a state update confirmation or using a useEffect.

Comment on lines 49 to 50
setError('Failed to impersonate user. Please try again.');
setIsLoading(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: setIsLoading(false) is not called if clearSession() fails. Consider using try/finally to ensure loading state is always reset.

Copy link
Member

@FelixMalfait FelixMalfait left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

@FelixMalfait FelixMalfait merged commit f8f3945 into main Dec 16, 2024
18 of 19 checks passed
@FelixMalfait FelixMalfait deleted the fix/sleep-before-redirect branch December 16, 2024 14:15
Copy link

Thanks @AMoreaux for your contribution!
This marks your 26th PR on the repo. You're top 2% of all our contributors 🎉
See contributor page - Share on LinkedIn - Share on Twitter

Contributions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants