diff --git a/src/modules/auth.ts b/src/modules/auth.ts index 081bf77..2d734ac 100644 --- a/src/modules/auth.ts +++ b/src/modules/auth.ts @@ -31,7 +31,7 @@ export function createAuthModule( }, /** - * Redirects the user to the Base44 login page + * Redirects the user to the app's login page * @param {string} nextUrl - URL to redirect to after successful login * @throws {Error} When not in a browser environment */ @@ -47,9 +47,7 @@ export function createAuthModule( const redirectUrl = nextUrl || window.location.href; // Build the login URL - const loginUrl = `${serverUrl}/login?from_url=${encodeURIComponent( - redirectUrl - )}&app_id=${appId}`; + const loginUrl = `/login?from_url=${encodeURIComponent(redirectUrl)}`; // Redirect to the login page window.location.href = loginUrl; @@ -57,11 +55,11 @@ export function createAuthModule( /** * Logout the current user - * Removes the token from localStorage and optionally redirects to a URL - * @param redirectUrl - Optional URL to redirect to after logout + * Removes the token from localStorage and optionally redirects to a URL or reloads the page + * @param redirectUrl - Optional URL to redirect to after logout. Reloads the page if not provided * @returns {Promise} */ - async logout(redirectUrl?: string) { + logout(redirectUrl?: string) { // Remove token from axios headers delete axios.defaults.headers.common["Authorization"]; @@ -75,11 +73,13 @@ export function createAuthModule( } // Redirect if a URL is provided - if (redirectUrl && typeof window !== "undefined") { - window.location.href = redirectUrl; + if (typeof window !== "undefined") { + if (redirectUrl) { + window.location.href = redirectUrl; + } else { + window.location.reload(); + } } - - return Promise.resolve(); }, /** diff --git a/tests/unit/auth.test.js b/tests/unit/auth.test.js index 98016ae..603c064 100644 --- a/tests/unit/auth.test.js +++ b/tests/unit/auth.test.js @@ -149,7 +149,7 @@ describe('Auth Module', () => { // Verify the redirect URL was set correctly expect(mockLocation.href).toBe( - `${serverUrl}/login?from_url=${encodeURIComponent(nextUrl)}&app_id=${appId}` + `/login?from_url=${encodeURIComponent(nextUrl)}` ); // Restore window @@ -169,7 +169,7 @@ describe('Auth Module', () => { // Verify the redirect URL uses current URL expect(mockLocation.href).toBe( - `${serverUrl}/login?from_url=${encodeURIComponent(currentUrl)}&app_id=${appId}` + `/login?from_url=${encodeURIComponent(currentUrl)}` ); // Restore window @@ -192,7 +192,7 @@ describe('Auth Module', () => { expect(scope.isDone()).toBe(true); // Call logout - await base44.auth.logout(); + base44.auth.logout(); // Mock another me() call to verify no Authorization header is sent scope.get(`/api/apps/${appId}/entities/User/me`) @@ -214,7 +214,10 @@ describe('Auth Module', () => { }; const originalWindow = global.window; global.window = { - localStorage: mockLocalStorage + localStorage: mockLocalStorage, + location: { + reload: vi.fn() + } }; // Set a token to localStorage first @@ -222,7 +225,7 @@ describe('Auth Module', () => { expect(mockLocalStorage.setItem).toHaveBeenCalledWith('base44_access_token', 'test-token'); // Call logout - await base44.auth.logout(); + base44.auth.logout(); // Verify token was removed from localStorage expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('base44_access_token'); @@ -241,11 +244,14 @@ describe('Auth Module', () => { const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}); const originalWindow = global.window; global.window = { - localStorage: mockLocalStorage + localStorage: mockLocalStorage, + location: { + reload: vi.fn() + } }; // Call logout - should not throw - await expect(base44.auth.logout()).resolves.toBeUndefined(); + base44.auth.logout(); // Verify error was logged expect(consoleSpy).toHaveBeenCalledWith('Failed to remove token from localStorage:', expect.any(Error)); @@ -264,7 +270,7 @@ describe('Auth Module', () => { }; const redirectUrl = 'https://example.com/logout-success'; - await base44.auth.logout(redirectUrl); + base44.auth.logout(redirectUrl); // Verify redirect expect(mockLocation.href).toBe(redirectUrl); @@ -272,6 +278,26 @@ describe('Auth Module', () => { // Restore window global.window = originalWindow; }); + + test('should reload page when no redirect URL is provided', async () => { + // Mock window object with reload function + const mockReload = vi.fn(); + const originalWindow = global.window; + global.window = { + location: { + reload: mockReload + } + }; + + // Call logout without redirect URL + base44.auth.logout(); + + // Verify page reload was called + expect(mockReload).toHaveBeenCalledTimes(1); + + // Restore window + global.window = originalWindow; + }); }); describe('setToken()', () => {