Skip to content

Commit

Permalink
Fix woo express login url (#88608)
Browse files Browse the repository at this point in the history
* Fix woo express login url

* Add back aff and vid

* Pass wccom param and fix login link
  • Loading branch information
chihsuan authored Mar 20, 2024
1 parent 811b835 commit 47630e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
51 changes: 19 additions & 32 deletions client/landing/stepper/declarative-flow/trial-wooexpress-flow.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -143,7 +130,7 @@ const wooexpress: Flow = {
}

if ( ! userIsLoggedIn ) {
const logInUrl = getStartUrl();
const logInUrl = getLoginUrl();
window.location.assign( logInUrl );
}
}, [] );
Expand Down
4 changes: 4 additions & 0 deletions client/signup/steps/user/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 47630e9

Please sign in to comment.