Skip to content

Commit

Permalink
Fix back redirection from Woo connect switch user flow
Browse files Browse the repository at this point in the history
  • Loading branch information
vishnugopal committed May 17, 2024
1 parent ff6f275 commit 01d2dcf
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion client/layout/masterbar/woo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,34 @@ import './typekit';
import './woo.scss';

const WooOauthMasterbar = () => {
function onClick() {
function goBack() {
window.history.back();
}

function onClick() {
// When the URL that initiates login is the connection page (during a switch user flow),
// we need to find the deeply nested home_url parameter to redirect back to the
// extensions page. This is because otherwise history back() will just redirect back to
// login as we still don't have a user. At any stage if we're not able to find a
// parameter, we give up and go back.
try {
const url = new URL( window.location.href );
const redirectURL = new URL( url.searchParams.get( 'redirect_to' ) );
const siteURL = new URL( redirectURL.searchParams.get( 'redirect_uri' ) );
// We just get a path here, so we construct an example URL.
const nextURL = new URL( 'http://example.com' + siteURL.searchParams.get( 'next' ) );
const homeURL = nextURL.searchParams.get( 'home_url' );
if ( ! homeURL ) {
return goBack();
}
// add the extensions page path.
const finalRedirectURL = `${ homeURL }/wp-admin/admin.php?page=wc-admin&tab=my-subscriptions&path=%2Fextensions`;
window.location = finalRedirectURL;
} catch {
goBack();
}
}

const backNav = (
<li className="masterbar__woo-nav-item">
<Button className="masterbar__login-back-link" onClick={ onClick }>
Expand Down

0 comments on commit 01d2dcf

Please sign in to comment.