From 47630e9e2abea24c55503f1e2bbebd320949c2a2 Mon Sep 17 00:00:00 2001 From: Chi-Hsuan Huang Date: Wed, 20 Mar 2024 10:22:53 +0800 Subject: [PATCH] Fix woo express login url (#88608) * Fix woo express login url * Add back aff and vid * Pass wccom param and fix login link --- .../declarative-flow/trial-wooexpress-flow.ts | 51 +++++++------------ client/signup/steps/user/index.jsx | 4 ++ 2 files changed, 23 insertions(+), 32 deletions(-) diff --git a/client/landing/stepper/declarative-flow/trial-wooexpress-flow.ts b/client/landing/stepper/declarative-flow/trial-wooexpress-flow.ts index d0dedb9713cc7b..24a6edd3c246a8 100644 --- a/client/landing/stepper/declarative-flow/trial-wooexpress-flow.ts +++ b/client/landing/stepper/declarative-flow/trial-wooexpress-flow.ts @@ -1,14 +1,15 @@ import config from '@automattic/calypso-config'; import { useLocale } from '@automattic/i18n-utils'; import { useSelect, useDispatch } from '@wordpress/data'; +import { addQueryArgs } from '@wordpress/url'; import { useTranslate } from 'i18n-calypso'; import { useEffect } from 'react'; import { getLocaleFromQueryParam, getLocaleFromPathname } from 'calypso/boot/locale'; import recordGTMDatalayerEvent from 'calypso/lib/analytics/ad-tracking/woo/record-gtm-datalayer-event'; import { logToLogstash } from 'calypso/lib/logstash'; +import { login } from 'calypso/lib/paths'; import { useSiteSlugParam } from '../hooks/use-site-slug-param'; import { USER_STORE, ONBOARD_STORE, SITE_STORE } from '../stores'; -import { getLoginUrl } from '../utils/path'; import { recordSubmitStep } from './internals/analytics/record-submit-step'; import { STEPS } from './internals/steps'; import { AssignTrialResult } from './internals/steps-repository/assign-trial-plan/constants'; @@ -41,8 +42,6 @@ const wooexpress: Flow = { ); let result: AssertConditionResult = { state: AssertConditionState.SUCCESS }; - const flowName = this.name; - // There is a race condition where useLocale is reporting english, // despite there being a locale in the URL so we need to look it up manually. // We also need to support both query param and path suffix localized urls @@ -76,40 +75,28 @@ const wooexpress: Flow = { } catch {} } - const getStartUrl = () => { - let hasFlowParams = false; - const flowParams = new URLSearchParams(); - const queryParams = new URLSearchParams(); + const getLoginUrl = () => { + const redirectTo = addQueryArgs( '/setup/wooexpress', { + ...Object.fromEntries( queryParams ), + } ); - if ( vendorId ) { - queryParams.set( 'vid', vendorId ); - } + let logInUrl = login( { + locale, + redirectTo, + oauth2ClientId: queryParams.get( 'client_id' ) || undefined, + wccomFrom: queryParams.get( 'wccom-from' ) || undefined, + } ); if ( aff ) { - queryParams.set( 'aff', aff ); - } - - if ( locale && locale !== 'en' ) { - flowParams.set( 'locale', locale ); - hasFlowParams = true; + logInUrl = addQueryArgs( logInUrl, { aff } ); } - const redirectTarget = - `/setup/wooexpress` + - ( hasFlowParams ? encodeURIComponent( '?' + flowParams.toString() ) : '' ); - - let queryString = `redirect_to=${ redirectTarget }`; - - if ( queryParams.toString() ) { - queryString = `${ queryString }&${ queryParams.toString() }`; + if ( vendorId ) { + logInUrl = addQueryArgs( logInUrl, { + vid: vendorId, + } ); } - - const logInUrl = getLoginUrl( { - variationName: flowName, - locale, - } ); - - return `${ logInUrl }&${ queryString }`; + return logInUrl; }; // Despite sending a CHECKING state, this function gets called again with the @@ -143,7 +130,7 @@ const wooexpress: Flow = { } if ( ! userIsLoggedIn ) { - const logInUrl = getStartUrl(); + const logInUrl = getLoginUrl(); window.location.assign( logInUrl ); } }, [] ); diff --git a/client/signup/steps/user/index.jsx b/client/signup/steps/user/index.jsx index 67da35a9f9b867..756991c56ed4cb 100644 --- a/client/signup/steps/user/index.jsx +++ b/client/signup/steps/user/index.jsx @@ -115,6 +115,10 @@ function isOauth2RedirectValid( oauth2Redirect ) { return true; } + if ( oauth2Redirect.startsWith( '/setup/wooexpress' ) ) { + return true; + } + try { const url = new URL( oauth2Redirect ); return url.host === 'public-api.wordpress.com';