Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix back redirection from Woo connect switch user flow #90836

Merged
merged 3 commits into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion client/layout/masterbar/woo.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,32 @@ 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' ) );
// Validate the URL and get it back as a string.
const homeURL = new URL( nextURL.searchParams.get( 'home_url' ) ).toString();
// 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