6351c6c1c6
## Summary - Implement a return-to-path mechanism that preserves the user's intended destination across authentication flows (login, magic link, cross-domain redirects) - Uses layered persistence: Jotai atom (in-memory), sessionStorage with TTL (tab-switch resilience), URL query parameter (cross-domain propagation) - Includes path validation to prevent open redirects, automatic cleanup after successful login, and comprehensive test coverage - Replaces the unused `previousUrlState` with a robust `returnToPathState` system ## Test plan - [ ] Visit a deep link (e.g. `/objects/tasks`) while logged out — should redirect to login, then back to `/objects/tasks` after logging in - [ ] Visit an OAuth authorize link while logged out — should redirect to login, then to the authorize page - [ ] Test magic link flow: click sign-in link that opens new tab — should still redirect to original destination - [ ] Test cross-domain: visit `app.twenty.com/objects/tasks` — should preserve path through workspace domain redirect - [ ] Verify auth/onboarding paths are excluded from being saved as return paths - [ ] Verify return-to-path is cleared after successful navigation - [ ] All 215 existing `usePageChangeEffectNavigateLocation` tests pass Made with [Cursor](https://cursor.com)
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
import { useCallback } from 'react';
|
|
|
|
import { billingCheckoutSessionState } from '@/auth/states/billingCheckoutSessionState';
|
|
import { returnToPathState } from '@/auth/states/returnToPathState';
|
|
import { BILLING_CHECKOUT_SESSION_DEFAULT_VALUE } from '@/billing/constants/BillingCheckoutSessionDefaultValue';
|
|
import { isNonEmptyString } from '@sniptt/guards';
|
|
import { useStore } from 'jotai';
|
|
|
|
export const useBuildSearchParamsFromUrlSyncedStates = () => {
|
|
const store = useStore();
|
|
const buildSearchParamsFromUrlSyncedStates = useCallback(async () => {
|
|
const billingCheckoutSession = store.get(billingCheckoutSessionState.atom);
|
|
const returnToPath = store.get(returnToPathState.atom);
|
|
|
|
const output = {
|
|
...(billingCheckoutSession !== BILLING_CHECKOUT_SESSION_DEFAULT_VALUE
|
|
? {
|
|
billingCheckoutSession: JSON.stringify(billingCheckoutSession),
|
|
}
|
|
: {}),
|
|
...(isNonEmptyString(returnToPath) ? { returnToPath } : {}),
|
|
};
|
|
|
|
return output;
|
|
}, [store]);
|
|
|
|
return {
|
|
buildSearchParamsFromUrlSyncedStates,
|
|
};
|
|
};
|