Skip to content

Commit 1c7c943

Browse files
authored
Merge af50265 into f03e949
2 parents f03e949 + af50265 commit 1c7c943

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

‎src/modules/auth.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { AxiosInstance } from "axios";
2+
3+
import { removeAccessToken } from "../utils/auth-utils.js";
24
import {
35
AuthModuleOptions,
46
InternalAuthModule,
@@ -77,6 +79,12 @@ function loginViaPopup(
7779
window.addEventListener("message", onMessage);
7880
}
7981

82+
// "token" is the key the platform-v2 built-in SDK wrote.
83+
function removeStoredAccessTokens() {
84+
removeAccessToken({});
85+
removeAccessToken({ storageKey: "token" });
86+
}
87+
8088
/**
8189
* Creates the auth module for the Base44 SDK.
8290
*
@@ -155,7 +163,9 @@ export function createAuthModule(
155163
// Build the login URL
156164
const loginUrl = `${options.appBaseUrl}/login?from_url=${encodeURIComponent(redirectUrl)}`;
157165

158-
// Redirect to the login page
166+
// The login page must not boot with the token that just got us here,
167+
// or a rejected token redirects to /login forever.
168+
removeStoredAccessTokens();
159169
window.location.href = loginUrl;
160170
},
161171

@@ -202,16 +212,7 @@ export function createAuthModule(
202212

203213
// Only do the rest if in a browser environment
204214
if (typeof window !== "undefined") {
205-
// Remove token from localStorage
206-
if (window.localStorage) {
207-
try {
208-
window.localStorage.removeItem("base44_access_token");
209-
// Remove "token" that is set by the built-in SDK of platform version 2
210-
window.localStorage.removeItem("token");
211-
} catch (e) {
212-
console.error("Failed to remove token from localStorage:", e);
213-
}
214-
}
215+
removeStoredAccessTokens();
215216

216217
// Determine the from_url parameter
217218
const fromUrl = redirectUrl || window.location.href;

‎tests/unit/auth.test.js‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,21 @@ describe('Auth Module', () => {
287287
global.window = originalWindow;
288288
});
289289

290+
test('should drop the stored token before redirecting to login', () => {
291+
const mockLocalStorage = { removeItem: vi.fn(), getItem: vi.fn(), setItem: vi.fn(), clear: vi.fn() };
292+
const mockLocation = { href: 'https://example.com/current-page' };
293+
const originalWindow = global.window;
294+
global.window = { localStorage: mockLocalStorage, location: mockLocation };
295+
296+
base44.auth.redirectToLogin();
297+
298+
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('base44_access_token');
299+
expect(mockLocalStorage.removeItem).toHaveBeenCalledWith('token');
300+
expect(mockLocation.href).toContain('/login?from_url=');
301+
302+
global.window = originalWindow;
303+
});
304+
290305
test('should use appBaseUrl for login redirect when provided', () => {
291306
const customAppBaseUrl = 'https://custom-app.example.com';
292307
const clientWithCustomUrl = createClient({
@@ -419,7 +434,7 @@ describe('Auth Module', () => {
419434
base44.auth.logout();
420435

421436
// Verify error was logged
422-
expect(consoleSpy).toHaveBeenCalledWith('Failed to remove token from localStorage:', expect.any(Error));
437+
expect(consoleSpy).toHaveBeenCalledWith('Error removing token from local storage:', expect.any(Error));
423438

424439
// Restore
425440
consoleSpy.mockRestore();

0 commit comments

Comments
 (0)