@@ -7,17 +7,33 @@ describe('Auth Module', () => {
77 let scope ;
88 const appId = 'test-app-id' ;
99 const serverUrl = 'https://api.base44.com' ;
10-
10+ const appBaseUrl = 'https://api.base44.com' ;
11+
1112 beforeEach ( ( ) => {
13+ // Mock window.addEventListener and document for analytics module
14+ if ( typeof window !== 'undefined' ) {
15+ if ( ! window . addEventListener ) {
16+ window . addEventListener = vi . fn ( ) ;
17+ window . removeEventListener = vi . fn ( ) ;
18+ }
19+ }
20+ if ( typeof document === 'undefined' ) {
21+ global . document = {
22+ referrer : '' ,
23+ visibilityState : 'visible'
24+ } ;
25+ }
26+
1227 // Create a new client for each test
1328 base44 = createClient ( {
1429 serverUrl,
1530 appId,
31+ appBaseUrl,
1632 } ) ;
17-
33+
1834 // Create a nock scope for mocking API calls
1935 scope = nock ( serverUrl ) ;
20-
36+
2137 // Enable request debugging for Nock
2238 nock . disableNetConnect ( ) ;
2339 nock . emitter . on ( 'no match' , ( req ) => {
@@ -143,15 +159,15 @@ describe('Auth Module', () => {
143159 global . window = {
144160 location : mockLocation
145161 } ;
146-
162+
147163 const nextUrl = 'https://example.com/dashboard' ;
148164 base44 . auth . redirectToLogin ( nextUrl ) ;
149-
165+
150166 // Verify the redirect URL was set correctly
151167 expect ( mockLocation . href ) . toBe (
152- `/login?from_url=${ encodeURIComponent ( nextUrl ) } `
168+ `${ appBaseUrl } /login?from_url=${ encodeURIComponent ( nextUrl ) } `
153169 ) ;
154-
170+
155171 // Restore window
156172 global . window = originalWindow ;
157173 } ) ;
@@ -169,7 +185,7 @@ describe('Auth Module', () => {
169185
170186 // Verify the redirect URL uses current URL
171187 expect ( mockLocation . href ) . toBe (
172- `/login?from_url=${ encodeURIComponent ( currentUrl ) } `
188+ `${ appBaseUrl } /login?from_url=${ encodeURIComponent ( currentUrl ) } `
173189 ) ;
174190
175191 // Restore window
@@ -204,6 +220,12 @@ describe('Auth Module', () => {
204220 } ) ;
205221
206222 test ( 'should use relative URL for login redirect when appBaseUrl is not provided' , ( ) => {
223+ // Create a client without appBaseUrl
224+ const clientWithoutAppBaseUrl = createClient ( {
225+ serverUrl,
226+ appId,
227+ } ) ;
228+
207229 // Mock window.location
208230 const originalWindow = global . window ;
209231 const mockLocation = { href : '' , origin : 'https://current-app.com' } ;
@@ -212,7 +234,7 @@ describe('Auth Module', () => {
212234 } ;
213235
214236 const nextUrl = 'https://example.com/dashboard' ;
215- base44 . auth . redirectToLogin ( nextUrl ) ;
237+ clientWithoutAppBaseUrl . auth . redirectToLogin ( nextUrl ) ;
216238
217239 // Verify the redirect URL uses a relative path (no appBaseUrl prefix)
218240 expect ( mockLocation . href ) . toBe (
@@ -316,33 +338,33 @@ describe('Auth Module', () => {
316338 global . window = {
317339 location : mockLocation
318340 } ;
319-
341+
320342 const redirectUrl = 'https://example.com/logout-success' ;
321343 base44 . auth . logout ( redirectUrl ) ;
322-
323- // Verify redirect
324- expect ( mockLocation . href ) . toBe ( redirectUrl ) ;
325-
344+
345+ // Verify redirect to server-side logout endpoint with from_url parameter
346+ const expectedUrl = `${ appBaseUrl } /api/apps/auth/logout?from_url=${ encodeURIComponent ( redirectUrl ) } ` ;
347+ expect ( mockLocation . href ) . toBe ( expectedUrl ) ;
348+
326349 // Restore window
327350 global . window = originalWindow ;
328351 } ) ;
329352
330- test ( 'should reload page when no redirect URL is provided' , async ( ) => {
331- // Mock window object with reload function
332- const mockReload = vi . fn ( ) ;
353+ test ( 'should redirect to logout endpoint when no redirect URL is provided' , async ( ) => {
354+ // Mock window object
355+ const mockLocation = { href : 'https://example.com/current-page' } ;
333356 const originalWindow = global . window ;
334357 global . window = {
335- location : {
336- reload : mockReload
337- }
358+ location : mockLocation
338359 } ;
339-
360+
340361 // Call logout without redirect URL
341362 base44 . auth . logout ( ) ;
342-
343- // Verify page reload was called
344- expect ( mockReload ) . toHaveBeenCalledTimes ( 1 ) ;
345-
363+
364+ // Verify redirect to server-side logout endpoint with current page as from_url
365+ const expectedUrl = `${ appBaseUrl } /api/apps/auth/logout?from_url=${ encodeURIComponent ( 'https://example.com/current-page' ) } ` ;
366+ expect ( mockLocation . href ) . toBe ( expectedUrl ) ;
367+
346368 // Restore window
347369 global . window = originalWindow ;
348370 } ) ;
0 commit comments