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

TOOLBAR LOGOUT LOOP: init commit and test #1828

Merged
merged 10 commits into from
Mar 12, 2024
5 changes: 5 additions & 0 deletions .changeset/lucky-sheep-promise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/core': patch
---

Fixed issue where on a preview page the user could be stuck in a logout/login loop.
4 changes: 4 additions & 0 deletions packages/faustwp-core/src/hooks/useLogout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export function useLogout() {
return;
}

const isPreview = window?.location?.search?.includes('preview=true');

if (redirectUrl) {
window.location.assign(redirectUrl);
} else if (isPreview) {
window.location.assign('/');
} else {
window.location.reload();
}
Expand Down
16 changes: 16 additions & 0 deletions packages/faustwp-core/tests/hooks/useLogout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ describe('useLogout hook', () => {
fetchMock.restore();
});

it('calls window.location.assign to / if there is a preview url and no redirectUrl', async () => {
fetchMock.post(`/api/faust/auth/logout`, {
status: 205,
});

global.window.location.search = 'preview=true';

const { result } = renderHook(() => useLogout());

await act(() => result.current.logout());

expect(window.location.assign).toBeCalledWith('/');

fetchMock.restore();
});

it('calls window.location.assign if there is a redirect url', async () => {
fetchMock.post(`/api/faust/auth/logout`, {
status: 205,
Expand Down
Loading