Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/modules/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -47,21 +47,19 @@ 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;
},

/**
* 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<void>}
*/
async logout(redirectUrl?: string) {
logout(redirectUrl?: string) {
// Remove token from axios headers
delete axios.defaults.headers.common["Authorization"];

Expand All @@ -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();
},

/**
Expand Down
42 changes: 34 additions & 8 deletions tests/unit/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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`)
Expand All @@ -214,15 +214,18 @@ describe('Auth Module', () => {
};
const originalWindow = global.window;
global.window = {
localStorage: mockLocalStorage
localStorage: mockLocalStorage,
location: {
reload: vi.fn()
}
};

// Set a token to localStorage first
base44.auth.setToken('test-token', true);
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');
Expand All @@ -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));
Expand All @@ -264,14 +270,34 @@ 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);

// 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()', () => {
Expand Down