|
1 | 1 | /** |
2 | 2 | * 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. |
4 | 4 | */ |
5 | 5 |
|
| 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 | + |
6 | 19 | export interface AuthResponse { |
7 | 20 | ok: boolean; |
8 | 21 | error?: string; |
@@ -38,7 +51,7 @@ export async function sendVerificationEmail( |
38 | 51 | body.origin = origin; |
39 | 52 | } |
40 | 53 |
|
41 | | - const response = await fetch('/auth/send-verification', { |
| 54 | + const response = await fetch(`${getApiBase()}/auth/send-verification`, { |
42 | 55 | method: 'POST', |
43 | 56 | headers: { 'Content-Type': 'application/json' }, |
44 | 57 | credentials: 'include', |
@@ -69,7 +82,7 @@ export async function sendVerificationEmail( |
69 | 82 | */ |
70 | 83 | export async function verifyEmail(token: string): Promise<VerifyEmailResponse> { |
71 | 84 | try { |
72 | | - const response = await fetch('/auth/verify', { |
| 85 | + const response = await fetch(`${getApiBase()}/auth/verify`, { |
73 | 86 | method: 'POST', |
74 | 87 | headers: { 'Content-Type': 'application/json' }, |
75 | 88 | credentials: 'include', |
@@ -98,7 +111,7 @@ export async function verifyEmail(token: string): Promise<VerifyEmailResponse> { |
98 | 111 | */ |
99 | 112 | export async function getUserStatus(): Promise<UserStatusResponse> { |
100 | 113 | try { |
101 | | - const response = await fetch('/auth/status', { |
| 114 | + const response = await fetch(`${getApiBase()}/auth/status`, { |
102 | 115 | method: 'GET', |
103 | 116 | headers: { 'Content-Type': 'application/json' }, |
104 | 117 | credentials: 'include', |
@@ -138,7 +151,7 @@ export async function requestPasswordReset( |
138 | 151 | body.origin = origin; |
139 | 152 | } |
140 | 153 |
|
141 | | - const response = await fetch('/auth/request-reset', { |
| 154 | + const response = await fetch(`${getApiBase()}/auth/request-reset`, { |
142 | 155 | method: 'POST', |
143 | 156 | headers: { 'Content-Type': 'application/json' }, |
144 | 157 | credentials: 'include', |
@@ -169,7 +182,7 @@ export async function resetPassword( |
169 | 182 | newPassword: string |
170 | 183 | ): Promise<AuthResponse> { |
171 | 184 | try { |
172 | | - const response = await fetch('/auth/reset-password', { |
| 185 | + const response = await fetch(`${getApiBase()}/auth/reset-password`, { |
173 | 186 | method: 'POST', |
174 | 187 | headers: { 'Content-Type': 'application/json' }, |
175 | 188 | credentials: 'include', |
|
0 commit comments