diff --git a/apps/manacore/apps/web/src/lib/api/config.ts b/apps/manacore/apps/web/src/lib/api/config.ts new file mode 100644 index 000000000..74d50d4da --- /dev/null +++ b/apps/manacore/apps/web/src/lib/api/config.ts @@ -0,0 +1,23 @@ +/** + * API Configuration + * Provides runtime-configurable URLs for API calls + */ + +import { browser } from '$app/environment'; + +/** + * Get the Mana Core Auth URL dynamically at runtime + * - Client-side: uses injected window variable (set by hooks.server.ts) + * - Server-side (SSR): uses environment variable + * - Falls back to localhost for local development + */ +export function getManaAuthUrl(): string { + if (browser && typeof window !== 'undefined') { + // Client-side: use injected window variable (set by hooks.server.ts) + const injectedUrl = (window as unknown as { __PUBLIC_MANA_CORE_AUTH_URL__?: string }) + .__PUBLIC_MANA_CORE_AUTH_URL__; + return injectedUrl || 'http://localhost:3001'; + } + // Server-side (SSR): use environment variable + return process.env.PUBLIC_MANA_CORE_AUTH_URL || 'http://localhost:3001'; +} diff --git a/apps/manacore/apps/web/src/lib/api/credits.ts b/apps/manacore/apps/web/src/lib/api/credits.ts index f69549952..f6e937b58 100644 --- a/apps/manacore/apps/web/src/lib/api/credits.ts +++ b/apps/manacore/apps/web/src/lib/api/credits.ts @@ -4,8 +4,7 @@ */ import { authStore } from '$lib/stores/auth.svelte'; - -const MANA_AUTH_URL = 'http://localhost:3001'; // TODO: Use PUBLIC_MANA_CORE_AUTH_URL from env +import { getManaAuthUrl } from './config'; // Types export interface CreditBalance { @@ -53,7 +52,7 @@ export interface CreditPurchase { async function fetchWithAuth(endpoint: string, options: RequestInit = {}): Promise { const token = await authStore.getAccessToken(); - const response = await fetch(`${MANA_AUTH_URL}${endpoint}`, { + const response = await fetch(`${getManaAuthUrl()}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', @@ -99,7 +98,7 @@ export const creditsService = { * Get available credit packages (public endpoint) */ async getPackages(): Promise { - const response = await fetch(`${MANA_AUTH_URL}/api/v1/credits/packages`); + const response = await fetch(`${getManaAuthUrl()}/api/v1/credits/packages`); if (!response.ok) { throw new Error('Failed to fetch packages'); } diff --git a/apps/manacore/apps/web/src/lib/api/feedback.ts b/apps/manacore/apps/web/src/lib/api/feedback.ts index 946142c01..2969d9ecd 100644 --- a/apps/manacore/apps/web/src/lib/api/feedback.ts +++ b/apps/manacore/apps/web/src/lib/api/feedback.ts @@ -4,11 +4,10 @@ import { createFeedbackService } from '@manacore/shared-feedback-service'; import { authStore } from '$lib/stores/auth.svelte'; - -const MANA_AUTH_URL = 'http://localhost:3001'; // TODO: Use PUBLIC_MANA_CORE_AUTH_URL from env +import { getManaAuthUrl } from './config'; export const feedbackService = createFeedbackService({ - apiUrl: MANA_AUTH_URL, + apiUrl: getManaAuthUrl(), appId: 'manacore', getAuthToken: async () => authStore.getAccessToken(), }); diff --git a/apps/manacore/apps/web/src/lib/api/gifts.ts b/apps/manacore/apps/web/src/lib/api/gifts.ts index cfa6f8e06..0b422d695 100644 --- a/apps/manacore/apps/web/src/lib/api/gifts.ts +++ b/apps/manacore/apps/web/src/lib/api/gifts.ts @@ -4,8 +4,7 @@ */ import { authStore } from '$lib/stores/auth.svelte'; - -const MANA_AUTH_URL = 'http://localhost:3001'; // TODO: Use PUBLIC_MANA_CORE_AUTH_URL from env +import { getManaAuthUrl } from './config'; // Types export interface GiftCodeInfo { @@ -82,7 +81,7 @@ export interface CreateGiftRequest { // Helper function for public requests (no auth required) async function fetchPublic(endpoint: string): Promise { - const response = await fetch(`${MANA_AUTH_URL}${endpoint}`, { + const response = await fetch(`${getManaAuthUrl()}${endpoint}`, { headers: { 'Content-Type': 'application/json', }, @@ -100,7 +99,7 @@ async function fetchPublic(endpoint: string): Promise { async function fetchWithAuth(endpoint: string, options: RequestInit = {}): Promise { const token = await authStore.getAccessToken(); - const response = await fetch(`${MANA_AUTH_URL}${endpoint}`, { + const response = await fetch(`${getManaAuthUrl()}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', diff --git a/apps/manacore/apps/web/src/lib/api/profile.ts b/apps/manacore/apps/web/src/lib/api/profile.ts index de568d46a..372a70128 100644 --- a/apps/manacore/apps/web/src/lib/api/profile.ts +++ b/apps/manacore/apps/web/src/lib/api/profile.ts @@ -4,8 +4,7 @@ */ import { authStore } from '$lib/stores/auth.svelte'; - -const MANA_AUTH_URL = 'http://localhost:3001'; +import { getManaAuthUrl } from './config'; // Types export interface UserProfile { @@ -44,7 +43,7 @@ export interface AvatarUploadUrlResponse { async function fetchWithAuth(endpoint: string, options: RequestInit = {}): Promise { const token = await authStore.getAccessToken(); - const response = await fetch(`${MANA_AUTH_URL}${endpoint}`, { + const response = await fetch(`${getManaAuthUrl()}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', diff --git a/apps/manacore/apps/web/src/lib/api/referrals.ts b/apps/manacore/apps/web/src/lib/api/referrals.ts index 4b3b6ed59..89fa48e6e 100644 --- a/apps/manacore/apps/web/src/lib/api/referrals.ts +++ b/apps/manacore/apps/web/src/lib/api/referrals.ts @@ -4,8 +4,7 @@ */ import { authStore } from '$lib/stores/auth.svelte'; - -const MANA_AUTH_URL = 'http://localhost:3001'; // TODO: Use PUBLIC_MANA_CORE_AUTH_URL from env +import { getManaAuthUrl } from './config'; // Types export interface ReferralStats { @@ -55,7 +54,7 @@ export interface ReferralValidation { async function fetchWithAuth(endpoint: string, options: RequestInit = {}): Promise { const token = await authStore.getAccessToken(); - const response = await fetch(`${MANA_AUTH_URL}${endpoint}`, { + const response = await fetch(`${getManaAuthUrl()}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', @@ -109,7 +108,7 @@ export const referralsService = { */ async validateCode(code: string): Promise { try { - const response = await fetch(`${MANA_AUTH_URL}/api/v1/referrals/validate/${code}`); + const response = await fetch(`${getManaAuthUrl()}/api/v1/referrals/validate/${code}`); if (!response.ok) { return { valid: false, error: 'Invalid code' }; } diff --git a/apps/manacore/apps/web/src/lib/api/subscriptions.ts b/apps/manacore/apps/web/src/lib/api/subscriptions.ts index 848ea11ff..f4ef5bdce 100644 --- a/apps/manacore/apps/web/src/lib/api/subscriptions.ts +++ b/apps/manacore/apps/web/src/lib/api/subscriptions.ts @@ -4,8 +4,7 @@ */ import { authStore } from '$lib/stores/auth.svelte'; - -const MANA_AUTH_URL = 'http://localhost:3001'; +import { getManaAuthUrl } from './config'; // Types export interface SubscriptionPlan { @@ -60,7 +59,7 @@ export interface CurrentSubscription { async function fetchWithAuth(endpoint: string, options: RequestInit = {}): Promise { const token = await authStore.getAccessToken(); - const response = await fetch(`${MANA_AUTH_URL}${endpoint}`, { + const response = await fetch(`${getManaAuthUrl()}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', @@ -83,7 +82,7 @@ export const subscriptionsService = { * Get all available plans (public) */ async getPlans(): Promise { - const response = await fetch(`${MANA_AUTH_URL}/api/v1/subscriptions/plans`); + const response = await fetch(`${getManaAuthUrl()}/api/v1/subscriptions/plans`); if (!response.ok) { throw new Error('Failed to fetch plans'); }