Skip to content

Commit 697ecfd

Browse files
authored
reload page after logout if no redirect (#4)
1 parent cc05e11 commit 697ecfd

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

src/modules/auth.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function createAuthModule(
3131
},
3232

3333
/**
34-
* Redirects the user to the Base44 login page
34+
* Redirects the user to the app's login page
3535
* @param {string} nextUrl - URL to redirect to after successful login
3636
* @throws {Error} When not in a browser environment
3737
*/
@@ -47,21 +47,19 @@ export function createAuthModule(
4747
const redirectUrl = nextUrl || window.location.href;
4848

4949
// Build the login URL
50-
const loginUrl = `${serverUrl}/login?from_url=${encodeURIComponent(
51-
redirectUrl
52-
)}&app_id=${appId}`;
50+
const loginUrl = `/login?from_url=${encodeURIComponent(redirectUrl)}`;
5351

5452
// Redirect to the login page
5553
window.location.href = loginUrl;
5654
},
5755

5856
/**
5957
* 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
58+
* Removes the token from localStorage and optionally redirects to a URL or reloads the page
59+
* @param redirectUrl - Optional URL to redirect to after logout. Reloads the page if not provided
6260
* @returns {Promise<void>}
6361
*/
64-
async logout(redirectUrl?: string) {
62+
logout(redirectUrl?: string) {
6563
// Remove token from axios headers
6664
delete axios.defaults.headers.common["Authorization"];
6765

@@ -75,11 +73,13 @@ export function createAuthModule(
7573
}
7674

7775
// Redirect if a URL is provided
78-
if (redirectUrl && typeof window !== "undefined") {
79-
window.location.href = redirectUrl;
76+
if (typeof window !== "undefined") {
77+
if (redirectUrl) {
78+
window.location.href = redirectUrl;
79+
} else {
80+
window.location.reload();
81+
}
8082
}
81-
82-
return Promise.resolve();
8383
},
8484

8585
/**

tests/unit/auth.test.js

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe('Auth Module', () => {
149149

150150
// Verify the redirect URL was set correctly
151151
expect(mockLocation.href).toBe(
152-
`${serverUrl}/login?from_url=${encodeURIComponent(nextUrl)}&app_id=${appId}`
152+
`/login?from_url=${encodeURIComponent(nextUrl)}`
153153
);
154154

155155
// Restore window
@@ -169,7 +169,7 @@ describe('Auth Module', () => {
169169

170170
// Verify the redirect URL uses current URL
171171
expect(mockLocation.href).toBe(
172-
`${serverUrl}/login?from_url=${encodeURIComponent(currentUrl)}&app_id=${appId}`
172+
`/login?from_url=${encodeURIComponent(currentUrl)}`
173173
);
174174

175175
// Restore window
@@ -192,7 +192,7 @@ describe('Auth Module', () => {
192192
expect(scope.isDone()).toBe(true);
193193

194194
// Call logout
195-
await base44.auth.logout();
195+
base44.auth.logout();
196196

197197
// Mock another me() call to verify no Authorization header is sent
198198
scope.get(`/api/apps/${appId}/entities/User/me`)
@@ -214,15 +214,18 @@ 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
221224
base44.auth.setToken('test-token', true);
222225
expect(mockLocalStorage.setItem).toHaveBeenCalledWith('base44_access_token', 'test-token');
223226

224227
// Call logout
225-
await base44.auth.logout();
228+
base44.auth.logout();
226229

227230
// Verify token was removed from localStorage
228231
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('base44_access_token');
@@ -241,11 +244,14 @@ 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
248-
await expect(base44.auth.logout()).resolves.toBeUndefined();
254+
base44.auth.logout();
249255

250256
// Verify error was logged
251257
expect(consoleSpy).toHaveBeenCalledWith('Failed to remove token from localStorage:', expect.any(Error));
@@ -264,14 +270,34 @@ describe('Auth Module', () => {
264270
};
265271

266272
const redirectUrl = 'https://example.com/logout-success';
267-
await base44.auth.logout(redirectUrl);
273+
base44.auth.logout(redirectUrl);
268274

269275
// Verify redirect
270276
expect(mockLocation.href).toBe(redirectUrl);
271277

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+
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)