Skip to content

Commit c6669a9

Browse files
committed
default appBaseUrl
1 parent 550e7d2 commit c6669a9

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

src/client.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ export function createClient(config: CreateClientConfig): Base44Client {
7070
headers: optionalHeaders,
7171
} = config;
7272

73+
// Normalize appBaseUrl to always be a string (empty if not provided or invalid)
74+
const normalizedAppBaseUrl = typeof appBaseUrl === "string" ? appBaseUrl : "";
75+
7376
const socketConfig: RoomsSocketConfig = {
7477
serverUrl,
7578
mountPath: "/ws-user-apps/socket.io/",
@@ -135,7 +138,7 @@ export function createClient(config: CreateClientConfig): Base44Client {
135138
functionsAxiosClient,
136139
appId,
137140
{
138-
appBaseUrl,
141+
appBaseUrl: normalizedAppBaseUrl,
139142
serverUrl,
140143
}
141144
);

src/modules/auth.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export function createAuthModule(
4949
: window.location.href;
5050

5151
// Build the login URL
52-
const loginUrl = `${
53-
options.appBaseUrl ?? ""
54-
}/login?from_url=${encodeURIComponent(redirectUrl)}`;
52+
const loginUrl = `${options.appBaseUrl}/login?from_url=${encodeURIComponent(redirectUrl)}`;
5553

5654
// Redirect to the login page
5755
window.location.href = loginUrl;

src/modules/auth.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ export interface ResetPasswordParams {
9898
export interface AuthModuleOptions {
9999
/** Server URL for API requests. */
100100
serverUrl: string;
101-
/** Optional base URL for the app (used for login redirects). */
102-
appBaseUrl?: string;
101+
/** Base URL for the app (used for login redirects). */
102+
appBaseUrl: string;
103103
}
104104

105105
/**

tests/unit/client.test.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ describe('Client Creation', () => {
6464
serviceToken: 'service-token-123',
6565
requiresAuth: true,
6666
});
67-
67+
6868
expect(client).toBeDefined();
6969
expect(client.entities).toBeDefined();
7070
expect(client.integrations).toBeDefined();
@@ -78,6 +78,59 @@ describe('Client Creation', () => {
7878

7979
});
8080

81+
describe('appBaseUrl Normalization', () => {
82+
test('should use appBaseUrl when provided as a string', () => {
83+
const customAppBaseUrl = 'https://custom-app.example.com';
84+
const client = createClient({
85+
appId: 'test-app-id',
86+
appBaseUrl: customAppBaseUrl,
87+
});
88+
89+
// Mock window.location
90+
const originalWindow = global.window;
91+
const mockLocation = { href: '', origin: 'https://current-app.com' };
92+
global.window = {
93+
location: mockLocation
94+
};
95+
96+
const nextUrl = 'https://example.com/dashboard';
97+
client.auth.redirectToLogin(nextUrl);
98+
99+
// Verify the redirect URL uses the custom appBaseUrl
100+
expect(mockLocation.href).toBe(
101+
`${customAppBaseUrl}/login?from_url=${encodeURIComponent(nextUrl)}`
102+
);
103+
104+
// Restore window
105+
global.window = originalWindow;
106+
});
107+
108+
test('should normalize appBaseUrl to empty string when not provided', () => {
109+
const client = createClient({
110+
appId: 'test-app-id',
111+
// appBaseUrl not provided
112+
});
113+
114+
// Mock window.location
115+
const originalWindow = global.window;
116+
const mockLocation = { href: '', origin: 'https://current-app.com' };
117+
global.window = {
118+
location: mockLocation
119+
};
120+
121+
const nextUrl = 'https://example.com/dashboard';
122+
client.auth.redirectToLogin(nextUrl);
123+
124+
// Verify the redirect URL uses empty string (relative path)
125+
expect(mockLocation.href).toBe(
126+
`/login?from_url=${encodeURIComponent(nextUrl)}`
127+
);
128+
129+
// Restore window
130+
global.window = originalWindow;
131+
});
132+
});
133+
81134
describe('createClientFromRequest', () => {
82135
test('should create client from request with all headers', () => {
83136
const mockRequest = {

0 commit comments

Comments
 (0)