4b127451b4
<!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/twentyhq/twenty/pull/22641?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { isAppEffectRedirectEnabledState } from '@/app/states/isAppEffectRedirectEnabledState';
|
|
import { useAuth } from '@/auth/hooks/useAuth';
|
|
import { tokenPairState } from '@/auth/states/tokenPairState';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { useSetAtomState } from '@/ui/utilities/state/jotai/hooks/useSetAtomState';
|
|
import { useLingui } from '@lingui/react/macro';
|
|
import { AppPath } from 'twenty-shared/types';
|
|
import { useNavigateApp } from '~/hooks/useNavigateApp';
|
|
|
|
export const useVerifyLogin = () => {
|
|
const { enqueueErrorSnackBar } = useSnackBar();
|
|
const navigate = useNavigateApp();
|
|
const setTokenPair = useSetAtomState(tokenPairState);
|
|
const setIsAppEffectRedirectEnabled = useSetAtomState(
|
|
isAppEffectRedirectEnabledState,
|
|
);
|
|
const { getAuthTokensFromLoginToken } = useAuth();
|
|
const { t } = useLingui();
|
|
|
|
const verifyLoginToken = async (loginToken: string) => {
|
|
// Keeps PageChangeEffect from consuming returnToPath mid token swap
|
|
setIsAppEffectRedirectEnabled(false);
|
|
setTokenPair(null);
|
|
try {
|
|
await getAuthTokensFromLoginToken(loginToken);
|
|
} catch {
|
|
enqueueErrorSnackBar({
|
|
message: t`Authentication failed`,
|
|
});
|
|
navigate(AppPath.SignInUp);
|
|
} finally {
|
|
setIsAppEffectRedirectEnabled(true);
|
|
}
|
|
};
|
|
|
|
return { verifyLoginToken };
|
|
};
|