Skip to content

Commit 38a5836

Browse files
committed
add base-url api shimming
1 parent 56c3027 commit 38a5836

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

packages/common-ui/src/services/authAPI.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
/**
22
* Authentication API service for interacting with Express backend auth endpoints.
3-
* Uses relative paths (same-origin) to avoid ENV coupling.
3+
* Uses configurable API base path from environment or falls back to relative paths.
44
*/
55

6+
// Get API base path from environment, defaulting to empty string for relative paths
7+
const getApiBase = (): string => {
8+
if (typeof import.meta !== 'undefined' && import.meta.env) {
9+
const base = import.meta.env.VITE_API_BASE_URL;
10+
if (base) {
11+
// Remove trailing slash if present, ensure leading slash
12+
const cleaned = base.replace(/\/$/, '');
13+
return cleaned.startsWith('/') ? cleaned : `/${cleaned}`;
14+
}
15+
}
16+
return '';
17+
};
18+
619
export interface AuthResponse {
720
ok: boolean;
821
error?: string;
@@ -38,7 +51,7 @@ export async function sendVerificationEmail(
3851
body.origin = origin;
3952
}
4053

41-
const response = await fetch('/auth/send-verification', {
54+
const response = await fetch(`${getApiBase()}/auth/send-verification`, {
4255
method: 'POST',
4356
headers: { 'Content-Type': 'application/json' },
4457
credentials: 'include',
@@ -69,7 +82,7 @@ export async function sendVerificationEmail(
6982
*/
7083
export async function verifyEmail(token: string): Promise<VerifyEmailResponse> {
7184
try {
72-
const response = await fetch('/auth/verify', {
85+
const response = await fetch(`${getApiBase()}/auth/verify`, {
7386
method: 'POST',
7487
headers: { 'Content-Type': 'application/json' },
7588
credentials: 'include',
@@ -98,7 +111,7 @@ export async function verifyEmail(token: string): Promise<VerifyEmailResponse> {
98111
*/
99112
export async function getUserStatus(): Promise<UserStatusResponse> {
100113
try {
101-
const response = await fetch('/auth/status', {
114+
const response = await fetch(`${getApiBase()}/auth/status`, {
102115
method: 'GET',
103116
headers: { 'Content-Type': 'application/json' },
104117
credentials: 'include',
@@ -138,7 +151,7 @@ export async function requestPasswordReset(
138151
body.origin = origin;
139152
}
140153

141-
const response = await fetch('/auth/request-reset', {
154+
const response = await fetch(`${getApiBase()}/auth/request-reset`, {
142155
method: 'POST',
143156
headers: { 'Content-Type': 'application/json' },
144157
credentials: 'include',
@@ -169,7 +182,7 @@ export async function resetPassword(
169182
newPassword: string
170183
): Promise<AuthResponse> {
171184
try {
172-
const response = await fetch('/auth/reset-password', {
185+
const response = await fetch(`${getApiBase()}/auth/reset-password`, {
173186
method: 'POST',
174187
headers: { 'Content-Type': 'application/json' },
175188
credentials: 'include',

0 commit comments

Comments
 (0)