fix(captcha): simplify captcha token refresh logic (#12876)
Fix https://twenty-v7.sentry.io/issues/6686753138/events/latest/?environment=prod&project=4507072499810304&query=is%3Aunresolved%20issue.priority%3A%5Bhigh%2C%20medium%5D&referrer=latest-event&sort=date&stream_index=1 Fix captcha invalid
This commit is contained in:
@ -464,19 +464,27 @@ export const useAuth = () => {
|
|||||||
|
|
||||||
const handleCredentialsSignUp = useCallback(
|
const handleCredentialsSignUp = useCallback(
|
||||||
async (email: string, password: string, captchaToken?: string) => {
|
async (email: string, password: string, captchaToken?: string) => {
|
||||||
return signUp({
|
const signUpResult = await signUp({
|
||||||
variables: { email, password, captchaToken },
|
variables: { email, password, captchaToken },
|
||||||
onCompleted: async (data) => {
|
|
||||||
handleSetAuthTokens(data.signUp.tokens);
|
|
||||||
const { user } = await loadCurrentUser();
|
|
||||||
|
|
||||||
if (countAvailableWorkspaces(user.availableWorkspaces) === 0) {
|
|
||||||
return createWorkspace();
|
|
||||||
}
|
|
||||||
|
|
||||||
setSignInUpStep(SignInUpStep.WorkspaceSelection);
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isDefined(signUpResult.errors)) {
|
||||||
|
throw signUpResult.errors;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!signUpResult.data?.signUp) {
|
||||||
|
throw new Error('No signUp result');
|
||||||
|
}
|
||||||
|
|
||||||
|
handleSetAuthTokens(signUpResult.data.signUp.tokens);
|
||||||
|
|
||||||
|
const { user } = await loadCurrentUser();
|
||||||
|
|
||||||
|
if (countAvailableWorkspaces(user.availableWorkspaces) === 0) {
|
||||||
|
return await createWorkspace({ newTab: false });
|
||||||
|
}
|
||||||
|
|
||||||
|
setSignInUpStep(SignInUpStep.WorkspaceSelection);
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
handleSetAuthTokens,
|
handleSetAuthTokens,
|
||||||
|
|||||||
@ -11,7 +11,7 @@ export const useSignUpInNewWorkspace = () => {
|
|||||||
|
|
||||||
const [signUpInNewWorkspaceMutation] = useSignUpInNewWorkspaceMutation();
|
const [signUpInNewWorkspaceMutation] = useSignUpInNewWorkspaceMutation();
|
||||||
|
|
||||||
const createWorkspace = () => {
|
const createWorkspace = ({ newTab } = { newTab: true }) => {
|
||||||
signUpInNewWorkspaceMutation({
|
signUpInNewWorkspaceMutation({
|
||||||
onCompleted: async (data) => {
|
onCompleted: async (data) => {
|
||||||
return await redirectToWorkspaceDomain(
|
return await redirectToWorkspaceDomain(
|
||||||
@ -20,7 +20,7 @@ export const useSignUpInNewWorkspace = () => {
|
|||||||
{
|
{
|
||||||
loginToken: data.signUpInNewWorkspace.loginToken.token,
|
loginToken: data.signUpInNewWorkspace.loginToken.token,
|
||||||
},
|
},
|
||||||
'_blank',
|
newTab ? '_blank' : '_self',
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
onError: (error: Error) => {
|
onError: (error: Error) => {
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import { captchaTokenState } from '@/captcha/states/captchaTokenState';
|
|||||||
import { isRequestingCaptchaTokenState } from '@/captcha/states/isRequestingCaptchaTokenState';
|
import { isRequestingCaptchaTokenState } from '@/captcha/states/isRequestingCaptchaTokenState';
|
||||||
import { isCaptchaRequiredForPath } from '@/captcha/utils/isCaptchaRequiredForPath';
|
import { isCaptchaRequiredForPath } from '@/captcha/utils/isCaptchaRequiredForPath';
|
||||||
import { captchaState } from '@/client-config/states/captchaState';
|
import { captchaState } from '@/client-config/states/captchaState';
|
||||||
import { useLocation } from 'react-router-dom';
|
|
||||||
import { CaptchaDriverType } from '~/generated-metadata/graphql';
|
import { CaptchaDriverType } from '~/generated-metadata/graphql';
|
||||||
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
|
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
|
||||||
|
|
||||||
@ -21,12 +20,10 @@ export const useRequestFreshCaptchaToken = () => {
|
|||||||
isRequestingCaptchaTokenState,
|
isRequestingCaptchaTokenState,
|
||||||
);
|
);
|
||||||
|
|
||||||
const location = useLocation();
|
|
||||||
|
|
||||||
const requestFreshCaptchaToken = useRecoilCallback(
|
const requestFreshCaptchaToken = useRecoilCallback(
|
||||||
({ snapshot }) =>
|
({ snapshot }) =>
|
||||||
async () => {
|
async () => {
|
||||||
if (!isCaptchaRequiredForPath(location.pathname)) {
|
if (!isCaptchaRequiredForPath(window.location.pathname)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,12 +44,6 @@ export const useRequestFreshCaptchaToken = () => {
|
|||||||
action: 'submit',
|
action: 'submit',
|
||||||
})
|
})
|
||||||
.then((token: string) => {
|
.then((token: string) => {
|
||||||
// TODO remove this log once debugged
|
|
||||||
// eslint-disable-next-line no-console
|
|
||||||
console.log(
|
|
||||||
'Google Recaptcha token generated at',
|
|
||||||
new Date().toISOString(),
|
|
||||||
);
|
|
||||||
setCaptchaToken(token);
|
setCaptchaToken(token);
|
||||||
setIsRequestingCaptchaToken(false);
|
setIsRequestingCaptchaToken(false);
|
||||||
});
|
});
|
||||||
@ -69,7 +60,7 @@ export const useRequestFreshCaptchaToken = () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[location.pathname, setCaptchaToken, setIsRequestingCaptchaToken],
|
[setCaptchaToken, setIsRequestingCaptchaToken],
|
||||||
);
|
);
|
||||||
|
|
||||||
return { requestFreshCaptchaToken };
|
return { requestFreshCaptchaToken };
|
||||||
|
|||||||
Reference in New Issue
Block a user