Onboarding - add nextPath logic after email verification (#12342)
Context : Plan choice [on pricing page on website](https://twenty.com/pricing) should redirect you the right plan on app /plan-required page (after sign in), thanks to query parameters and BillingCheckoutSessionState sync. With email verification, an other session starts at CTA click in verification email. Initial BillingCheckoutSessionState is lost and user can't submit to the plan he choose. Solution : Pass a nextPath query parameter in email verification link To test : - Modify .env to add IS_BILLING_ENABLED (+ reset db + sync billing) + IS_EMAIL_VERIFICATION_REQUIRED - Start test from this page http://app.localhost:3001/welcome?billingCheckoutSession={%22plan%22:%22ENTERPRISE%22,%22interval%22:%22Year%22,%22requirePaymentMethod%22:true} - After verification, check you arrive on /plan-required page with Enterprise plan on a yearly interval (default is Pro/monthly). closes https://github.com/twentyhq/twenty/issues/12288
This commit is contained in:
@ -1,9 +1,9 @@
|
||||
import { useRecoilCallback } from 'recoil';
|
||||
|
||||
import { isQueryParamInitializedState } from '@/app/states/isQueryParamInitializedState';
|
||||
import { billingCheckoutSessionState } from '@/auth/states/billingCheckoutSessionState';
|
||||
import { BillingCheckoutSession } from '@/auth/types/billingCheckoutSession.type';
|
||||
import { BILLING_CHECKOUT_SESSION_DEFAULT_VALUE } from '@/billing/constants/BillingCheckoutSessionDefaultValue';
|
||||
import deepEqual from 'deep-equal';
|
||||
|
||||
// Initialize state that are hydrated from query parameters
|
||||
// We used to use recoil-sync to do this, but it was causing issues with Firefox
|
||||
@ -11,52 +11,49 @@ export const useInitializeQueryParamState = () => {
|
||||
const initializeQueryParamState = useRecoilCallback(
|
||||
({ set, snapshot }) =>
|
||||
() => {
|
||||
const isInitialized = snapshot
|
||||
.getLoadable(isQueryParamInitializedState)
|
||||
.getValue();
|
||||
const handlers = {
|
||||
billingCheckoutSession: (value: string) => {
|
||||
const billingCheckoutSession = snapshot
|
||||
.getLoadable(billingCheckoutSessionState)
|
||||
.getValue();
|
||||
|
||||
if (!isInitialized) {
|
||||
const handlers = {
|
||||
billingCheckoutSession: (value: string) => {
|
||||
try {
|
||||
const parsedValue = JSON.parse(decodeURIComponent(value));
|
||||
try {
|
||||
const parsedValue = JSON.parse(decodeURIComponent(value));
|
||||
|
||||
if (
|
||||
typeof parsedValue === 'object' &&
|
||||
parsedValue !== null &&
|
||||
'plan' in parsedValue &&
|
||||
'interval' in parsedValue &&
|
||||
'requirePaymentMethod' in parsedValue
|
||||
) {
|
||||
set(
|
||||
billingCheckoutSessionState,
|
||||
parsedValue as BillingCheckoutSession,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
'Failed to parse billingCheckoutSession from URL',
|
||||
error,
|
||||
);
|
||||
if (
|
||||
typeof parsedValue === 'object' &&
|
||||
parsedValue !== null &&
|
||||
'plan' in parsedValue &&
|
||||
'interval' in parsedValue &&
|
||||
'requirePaymentMethod' in parsedValue &&
|
||||
!deepEqual(billingCheckoutSession, parsedValue)
|
||||
) {
|
||||
set(
|
||||
billingCheckoutSessionState,
|
||||
BILLING_CHECKOUT_SESSION_DEFAULT_VALUE,
|
||||
parsedValue as BillingCheckoutSession,
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const queryParams = new URLSearchParams(window.location.search);
|
||||
|
||||
for (const [paramName, handler] of Object.entries(handlers)) {
|
||||
const value = queryParams.get(paramName);
|
||||
if (value !== null) {
|
||||
handler(value);
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(
|
||||
'Failed to parse billingCheckoutSession from URL',
|
||||
error,
|
||||
);
|
||||
set(
|
||||
billingCheckoutSessionState,
|
||||
BILLING_CHECKOUT_SESSION_DEFAULT_VALUE,
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
set(isQueryParamInitializedState, true);
|
||||
const queryParams = new URLSearchParams(window.location.search);
|
||||
|
||||
for (const [paramName, handler] of Object.entries(handlers)) {
|
||||
const value = queryParams.get(paramName);
|
||||
if (value !== null) {
|
||||
handler(value);
|
||||
}
|
||||
}
|
||||
},
|
||||
[],
|
||||
|
||||
@ -1,6 +0,0 @@
|
||||
import { createState } from 'twenty-ui/utilities';
|
||||
|
||||
export const isQueryParamInitializedState = createState<boolean>({
|
||||
key: 'isQueryParamInitializedState',
|
||||
defaultValue: false,
|
||||
});
|
||||
@ -0,0 +1,6 @@
|
||||
import { createState } from 'twenty-ui/utilities';
|
||||
|
||||
export const verifyEmailNextPathState = createState<string | undefined>({
|
||||
key: 'verifyEmailNextPathState',
|
||||
defaultValue: undefined,
|
||||
});
|
||||
@ -4,12 +4,15 @@ import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/Snac
|
||||
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
||||
import { ApolloError } from '@apollo/client';
|
||||
|
||||
import { verifyEmailNextPathState } from '@/app/states/verifyEmailNextPathState';
|
||||
import { useVerifyLogin } from '@/auth/hooks/useVerifyLogin';
|
||||
import { useRedirectToWorkspaceDomain } from '@/domain-manager/hooks/useRedirectToWorkspaceDomain';
|
||||
import { Modal } from '@/ui/layout/modal/components/Modal';
|
||||
import { useLingui } from '@lingui/react/macro';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
import { useSetRecoilState } from 'recoil';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { useNavigateApp } from '~/hooks/useNavigateApp';
|
||||
import { getWorkspaceUrl } from '~/utils/getWorkspaceUrl';
|
||||
import { EmailVerificationSent } from '../sign-in-up/components/EmailVerificationSent';
|
||||
@ -22,8 +25,11 @@ export const VerifyEmailEffect = () => {
|
||||
const [searchParams] = useSearchParams();
|
||||
const [isError, setIsError] = useState(false);
|
||||
|
||||
const setVerifyEmailNextPath = useSetRecoilState(verifyEmailNextPathState);
|
||||
|
||||
const email = searchParams.get('email');
|
||||
const emailVerificationToken = searchParams.get('emailVerificationToken');
|
||||
const verifyEmailNextPath = searchParams.get('nextPath');
|
||||
|
||||
const navigate = useNavigateApp();
|
||||
const { redirectToWorkspaceDomain } = useRedirectToWorkspaceDomain();
|
||||
@ -58,6 +64,11 @@ export const VerifyEmailEffect = () => {
|
||||
loginToken: loginToken.token,
|
||||
});
|
||||
}
|
||||
|
||||
if (isDefined(verifyEmailNextPath)) {
|
||||
setVerifyEmailNextPath(verifyEmailNextPath);
|
||||
}
|
||||
|
||||
verifyLoginToken(loginToken.token);
|
||||
} catch (error) {
|
||||
const message: string =
|
||||
|
||||
@ -9,6 +9,7 @@ export const SIGN_UP = gql`
|
||||
$captchaToken: String
|
||||
$workspaceId: String
|
||||
$locale: String
|
||||
$verifyEmailNextPath: String
|
||||
) {
|
||||
signUp(
|
||||
email: $email
|
||||
@ -18,6 +19,7 @@ export const SIGN_UP = gql`
|
||||
captchaToken: $captchaToken
|
||||
workspaceId: $workspaceId
|
||||
locale: $locale
|
||||
verifyEmailNextPath: $verifyEmailNextPath
|
||||
) {
|
||||
loginToken {
|
||||
...AuthTokenFragment
|
||||
|
||||
@ -167,7 +167,7 @@ describe('useAuth', () => {
|
||||
const { result } = renderHooks();
|
||||
|
||||
await act(async () => {
|
||||
await result.current.signUpWithCredentials(email, password);
|
||||
await result.current.signUpWithCredentials({ email, password });
|
||||
});
|
||||
|
||||
expect(mocks[2].result).toHaveBeenCalled();
|
||||
|
||||
@ -397,13 +397,21 @@ export const useAuth = () => {
|
||||
}, [clearSession]);
|
||||
|
||||
const handleCredentialsSignUp = useCallback(
|
||||
async (
|
||||
email: string,
|
||||
password: string,
|
||||
workspaceInviteHash?: string,
|
||||
workspacePersonalInviteToken?: string,
|
||||
captchaToken?: string,
|
||||
) => {
|
||||
async ({
|
||||
email,
|
||||
password,
|
||||
workspaceInviteHash,
|
||||
workspacePersonalInviteToken,
|
||||
captchaToken,
|
||||
verifyEmailNextPath,
|
||||
}: {
|
||||
email: string;
|
||||
password: string;
|
||||
workspaceInviteHash?: string;
|
||||
workspacePersonalInviteToken?: string;
|
||||
captchaToken?: string;
|
||||
verifyEmailNextPath?: string;
|
||||
}) => {
|
||||
const signUpResult = await signUp({
|
||||
variables: {
|
||||
email,
|
||||
@ -415,6 +423,7 @@ export const useAuth = () => {
|
||||
...(workspacePublicData?.id
|
||||
? { workspaceId: workspacePublicData.id }
|
||||
: {}),
|
||||
verifyEmailNextPath,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@ -11,10 +11,12 @@ import {
|
||||
import { SignInUpMode } from '@/auth/types/signInUpMode';
|
||||
import { useReadCaptchaToken } from '@/captcha/hooks/useReadCaptchaToken';
|
||||
import { useRequestFreshCaptchaToken } from '@/captcha/hooks/useRequestFreshCaptchaToken';
|
||||
import { useBuildSearchParamsFromUrlSyncedStates } from '@/domain-manager/hooks/useBuildSearchParamsFromUrlSyncedStates';
|
||||
import { AppPath } from '@/types/AppPath';
|
||||
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
|
||||
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
||||
import { useRecoilState } from 'recoil';
|
||||
import { buildAppPathWithQueryParams } from '~/utils/buildAppPathWithQueryParams';
|
||||
import { isMatchingLocation } from '~/utils/isMatchingLocation';
|
||||
import { useAuth } from '../../hooks/useAuth';
|
||||
|
||||
@ -44,6 +46,9 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
|
||||
const { requestFreshCaptchaToken } = useRequestFreshCaptchaToken();
|
||||
const { readCaptchaToken } = useReadCaptchaToken();
|
||||
|
||||
const { buildSearchParamsFromUrlSyncedStates } =
|
||||
useBuildSearchParamsFromUrlSyncedStates();
|
||||
|
||||
const continueWithEmail = useCallback(() => {
|
||||
requestFreshCaptchaToken();
|
||||
setSignInUpStep(SignInUpStep.Email);
|
||||
@ -99,13 +104,19 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
|
||||
token,
|
||||
);
|
||||
} else {
|
||||
await signUpWithCredentials(
|
||||
data.email.toLowerCase().trim(),
|
||||
data.password,
|
||||
const verifyEmailNextPath = buildAppPathWithQueryParams(
|
||||
AppPath.PlanRequired,
|
||||
await buildSearchParamsFromUrlSyncedStates(),
|
||||
);
|
||||
|
||||
await signUpWithCredentials({
|
||||
email: data.email.toLowerCase().trim(),
|
||||
password: data.password,
|
||||
workspaceInviteHash,
|
||||
workspacePersonalInviteToken,
|
||||
token,
|
||||
);
|
||||
captchaToken: token,
|
||||
verifyEmailNextPath,
|
||||
});
|
||||
}
|
||||
} catch (err: any) {
|
||||
enqueueSnackBar(err?.message, {
|
||||
@ -124,6 +135,7 @@ export const useSignInUp = (form: UseFormReturn<Form>) => {
|
||||
workspacePersonalInviteToken,
|
||||
enqueueSnackBar,
|
||||
requestFreshCaptchaToken,
|
||||
buildSearchParamsFromUrlSyncedStates,
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user