SDK version: purecloud-platform-client-v2 260.0.0 (bundled with Vite, using src/purecloud-platform-client-v2/ApiClient.js)
I have a web widget that authenticates with PKCE using the popup flow:
client.updateAuthPopupConfiguration({
usePopup: true,
popupTimeout: 4000,
autoClosePopup: true,
autoClosePopupDelay: 30000,
popupTarget: "_blank",
popupWindowFeatures: "popup=true,width=600,height=500",
waitForLoginPromise: true
// other options left at default values
});
export async function authenticate() {
client.setEnvironment(environment);
try {
return await client.loginPKCEGrant(clientId, redirectUri, { target: organizationId, skipTest: true });
} catch (error) {
console.error('loginPKCEGrant error', error);
throw error;
}
}
I have made my oauth/callback following the github examples:
platform-client-sdk-javascript/build/samples at master · MyPureCloud/platform-client-sdk-javascript
My open window send a postMessage to an eventListener from the sdk.
window.opener.postMessage(
{ name: "gc_auth_popup", type: "message", hash: window.location.hash, search: window.location.search },
targetOrigin
);
When an API call returns 401, I call loginPKCEGrant again to get a new token. Usually i made my oAuth credentials only last 300 seconds to validate reauthentication.
Problem
- The first login always works.
- On every re-login after a 401, the browser sends more than one request to
/oauth/token with the same authorization code. One succeeds and the token is saved. The others fail with invalid_grant, and since that error response has no Access-Control-Allow-Origin header, the browser shows it as a CORS error.
- Listeners run in registration order, so the failing exchange is often the one tied to the current promise.
loginPKCEGrant rejects even though a valid token was just saved.
- The failing exchange also calls
_saveSettings({ accessToken: undefined }), so depending on which response arrives last, it can wipe the valid token.
- Each re-login adds one more exchange: 2 on the first re-login, 3 on the second, and so on.
Steps to reproduce
- Call
loginPKCEGrant with usePopup: true and complete the login.
- Call
loginPKCEGrant again (for example after a 401).
- Check the Network tab: there are two
POST /oauth/token requests with the same code, and one fails.
Possible cause
In ApiClient.js, _startAuthPopup registers an anonymous arrow function as the message listener:
// ApiClient.js:591
window.addEventListener('message', (event) => this._handleAuthPopupMessage(event, loginPopupConfiguration, resolve, reject));
But the cleanup code removes a different function reference:
// ApiClient.js:553, 621, 710
window.removeEventListener('message', this._handleAuthPopupMessage);
Since removeEventListener matches listeners by function reference, nothing is ever removed. Every popup login leaves a listener on window. On the next login, all the old listeners also receive the popup's postMessage and each one runs authorizePKCEGrant with the same code.
Current workaround
I'm not an expert on this topic, so I used AI to help me investigate. The workaround below intercepts window.addEventListener while loginPKCEGrant is running, to keep a reference to the listener the SDK registers, and removes it when the login finishes. With it, only one token exchange happens per login and the errors stopped:
const originalAddEventListener = window.addEventListener;
const sdkListeners = [];
window.addEventListener = function (type, listener, options) {
if (type === 'message' && String(listener).includes('_handleAuthPopupMessage')) {
sdkListeners.push({ listener, options });
}
return originalAddEventListener.call(this, type, listener, options);
};
try {
return await client.loginPKCEGrant(clientId, redirectUri, { target: organizationId, skipTest: true });
} finally {
window.addEventListener = originalAddEventListener;
sdkListeners.forEach(({ listener, options }) => window.removeEventListener('message', listener, options));
}
Questions
- Can anyone confirm that the SDK is not removing this listener properly? ( i dont have enough knowledge about this)
- If so, can it be fixed in a future release, so the workaround is no longer needed?
- Should I also open a ticket with Genesys Customer Care, or is this forum the right place for SDK issues?
Thanks!
#PlatformSDK------------------------------
Julio
------------------------------