🐛 fix(manacore-web): use runtime auth URL instead of hardcoded localhost

Added shared config.ts with getManaAuthUrl() that reads the auth URL from
window.__PUBLIC_MANA_CORE_AUTH_URL__ (injected by hooks.server.ts) instead
of hardcoded localhost:3001. Fixes API calls failing on production.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Till-JS 2026-02-14 12:47:40 +01:00
parent 5025bfa883
commit 8dad8a8302
7 changed files with 39 additions and 22 deletions

View file

@ -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';
}

View file

@ -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<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
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<CreditPackage[]> {
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');
}

View file

@ -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(),
});

View file

@ -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<T>(endpoint: string): Promise<T> {
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<T>(endpoint: string): Promise<T> {
async function fetchWithAuth<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
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',

View file

@ -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<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
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',

View file

@ -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<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
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<ReferralValidation> {
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' };
}

View file

@ -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<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
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<SubscriptionPlan[]> {
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');
}