From ba0a37c583e109d4184d391ecd05d07783c3de00 Mon Sep 17 00:00:00 2001 From: Joe Fusco Date: Wed, 21 Oct 2026 08:33:39 -0400 Subject: [PATCH] fix(core): stop the preview redirect loop when exchanging a code fails --- packages/faustwp-core/src/auth/authorize.ts | 21 ++++++++++++- .../auth/authorizeExchangeFailure.test.ts | 30 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/faustwp-core/tests/auth/authorizeExchangeFailure.test.ts diff --git a/packages/faustwp-core/src/auth/authorize.ts b/packages/faustwp-core/src/auth/authorize.ts index affc5ee00..b85e8f13d 100644 --- a/packages/faustwp-core/src/auth/authorize.ts +++ b/packages/faustwp-core/src/auth/authorize.ts @@ -1,7 +1,7 @@ import 'isomorphic-fetch'; import isString from 'lodash/isString.js'; import { getWpUrl } from '../lib/getWpUrl.js'; -import { getQueryParam, removeURLParam } from '../utils/index.js'; +import { errorLog, getQueryParam, removeURLParam } from '../utils/index.js'; import { fetchAccessToken, getAccessToken } from './client/accessToken.js'; export interface EnsureAuthorizationOptions { @@ -54,6 +54,25 @@ export async function ensureAuthorization( const token = await fetchAccessToken(code); + if (!token && code) { + /** + * WordPress already issued a code, so the exchange itself failed, for + * example because the frontend's credentials were rejected. Sending the + * user back for another code would loop forever, so stop here, and drop + * the code so a reload starts over. + */ + window.history.replaceState( + {}, + document.title, + removeURLParam(window.location.href, 'code'), + ); + errorLog( + 'Faust.js could not exchange the authorization code for an access token. Check your Next.js server logs.', + ); + + return {}; + } + if (!token) { return unauthorized; } diff --git a/packages/faustwp-core/tests/auth/authorizeExchangeFailure.test.ts b/packages/faustwp-core/tests/auth/authorizeExchangeFailure.test.ts new file mode 100644 index 000000000..cd413e052 --- /dev/null +++ b/packages/faustwp-core/tests/auth/authorizeExchangeFailure.test.ts @@ -0,0 +1,30 @@ +/** + * @jest-environment jsdom + */ + +import fetchMock from 'fetch-mock'; +import { ensureAuthorization } from '../../src/auth/authorize'; +import * as getWpUrl from '../../src/lib/getWpUrl'; +import * as log from '../../src/utils/log'; + +describe('ensureAuthorization() when exchanging a code fails', () => { + afterEach(() => { + fetchMock.restore(); + jest.restoreAllMocks(); + }); + + test('stops instead of sending the user back for another code', async () => { + jest.spyOn(getWpUrl, 'getWpUrl').mockReturnValue('http://test.local'); + const errorSpy = jest.spyOn(log, 'errorLog').mockImplementation(() => {}); + window.history.pushState({}, '', '/preview?p=1&preview=true&code=abc'); + fetchMock.get('begin:/api/faust/auth/token', { status: 403, body: {} }); + + const result = await ensureAuthorization({ + redirectUri: window.location.href, + }); + + expect(result).toEqual({}); + expect(window.location.search).toBe('?p=1&preview=true'); + expect(errorSpy).toHaveBeenCalled(); + }); +});