Skip to content

Commit 2ac021f

Browse files
committed
reload page after logout if no redirect
1 parent cc05e11 commit 2ac021f

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/modules/auth.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export function createAuthModule(
5757

5858
/**
5959
* Logout the current user
60-
* Removes the token from localStorage and optionally redirects to a URL
61-
* @param redirectUrl - Optional URL to redirect to after logout
60+
* Removes the token from localStorage and optionally redirects to a URL or reloads the page
61+
* @param redirectUrl - Optional URL to redirect to after logout. Reloads the page if not provided
6262
* @returns {Promise<void>}
6363
*/
6464
async logout(redirectUrl?: string) {
@@ -75,11 +75,13 @@ export function createAuthModule(
7575
}
7676

7777
// Redirect if a URL is provided
78-
if (redirectUrl && typeof window !== "undefined") {
79-
window.location.href = redirectUrl;
78+
if (typeof window !== "undefined") {
79+
if (redirectUrl) {
80+
window.location.href = redirectUrl;
81+
} else {
82+
window.location.reload();
83+
}
8084
}
81-
82-
return Promise.resolve();
8385
},
8486

8587
/**

tests/unit/auth.test.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ describe('Auth Module', () => {
214214
};
215215
const originalWindow = global.window;
216216
global.window = {
217-
localStorage: mockLocalStorage
217+
localStorage: mockLocalStorage,
218+
location: {
219+
reload: vi.fn()
220+
}
218221
};
219222

220223
// Set a token to localStorage first
@@ -241,7 +244,10 @@ describe('Auth Module', () => {
241244
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
242245
const originalWindow = global.window;
243246
global.window = {
244-
localStorage: mockLocalStorage
247+
localStorage: mockLocalStorage,
248+
location: {
249+
reload: vi.fn()
250+
}
245251
};
246252

247253
// Call logout - should not throw
@@ -272,6 +278,26 @@ describe('Auth Module', () => {
272278
// Restore window
273279
global.window = originalWindow;
274280
});
281+
282+
test('should reload page when no redirect URL is provided', async () => {
283+
// Mock window object with reload function
284+
const mockReload = vi.fn();
285+
const originalWindow = global.window;
286+
global.window = {
287+
location: {
288+
reload: mockReload
289+
}
290+
};
291+
292+
// Call logout without redirect URL
293+
await base44.auth.logout();
294+
295+
// Verify page reload was called
296+
expect(mockReload).toHaveBeenCalledTimes(1);
297+
298+
// Restore window
299+
global.window = originalWindow;
300+
});
275301
});
276302

277303
describe('setToken()', () => {

0 commit comments

Comments
 (0)