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

Handle expired or missing tokens for orders autosuggest #3308

Merged
merged 12 commits into from
Feb 15, 2023
Prev Previous commit
Next Next commit
Fix looping requests on non-authentication related errors.
  • Loading branch information
JakePT committed Feb 15, 2023
commit 0684c767e63a0c552791fe2fb52cc38771ea04e5
18 changes: 11 additions & 7 deletions assets/js/woocommerce/admin/orders/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies.
*/
import { render, useEffect, useState, WPElement } from '@wordpress/element';
import { render, useEffect, useRef, useState, WPElement } from '@wordpress/element';

/**
* Internal dependencies.
Expand Down Expand Up @@ -32,15 +32,19 @@ const AuthenticatedApiSearchProvider = ({ children }) => {
* State.
*/
const [credentials, setCredentials] = useState(null);
const [hasRefreshed, setHasRefreshed] = useState(false);

/**
* Refs.
*/
const hasRefreshed = useRef(false);

/**
* Refresh credentials on authentication errors.
*
* @returns {void}
*/
const onAuthError = () => {
if (hasRefreshed) {
if (hasRefreshed.current) {
setCredentials(null);
return;
}
Expand All @@ -52,7 +56,7 @@ const AuthenticatedApiSearchProvider = ({ children }) => {
.then((response) => response.text())
.then(setCredentials);

setHasRefreshed(true);
hasRefreshed.current = true;
};

/**
Expand All @@ -76,7 +80,7 @@ const AuthenticatedApiSearchProvider = ({ children }) => {
/**
* Render.
*/
return (
return credentials ? (
<ApiSearchProvider
apiEndpoint={apiEndpoint}
apiHost={apiHost}
Expand All @@ -85,9 +89,9 @@ const AuthenticatedApiSearchProvider = ({ children }) => {
requestIdBase={requestIdBase}
onAuthError={onAuthError}
>
{credentials ? children : null}
{children}
</ApiSearchProvider>
);
) : null;
};

/**
Expand Down