diff --git a/apps/mobile/app/(auth)/backend-setup.tsx b/apps/mobile/app/(auth)/backend-setup.tsx new file mode 100644 index 0000000..f9b9dcb --- /dev/null +++ b/apps/mobile/app/(auth)/backend-setup.tsx @@ -0,0 +1,157 @@ +import React, { useState, useEffect } from 'react'; +import { View, Text, TextInput, TouchableOpacity, StyleSheet, Alert } from 'react-native'; +import { router } from 'expo-router'; +import { getBackendUrl, setBackendUrl } from '../../lib/api'; + +export default function BackendSetup() { + const [url, setUrl] = useState(''); + const [loading, setLoading] = useState(true); + + useEffect(() => { + getBackendUrl().then((currentUrl: string) => { + setUrl(currentUrl); + setLoading(false); + }); + }, []); + + const handleSave = async () => { + try { + // Validate URL format + let validUrl = url.trim(); + if (!validUrl.startsWith('http://') && !validUrl.startsWith('https://')) { + validUrl = 'http://' + validUrl; + } + + // Remove trailing slash + validUrl = validUrl.replace(/\/$/, ''); + + await setBackendUrl(validUrl); + Alert.alert('Success', `Connected to ${validUrl}`, [ + { text: 'OK', onPress: () => router.replace('/(auth)/sign-in') }, + ]); + } catch (e) { + Alert.alert('Error', 'Failed to save backend URL'); + } + }; + + const useDefault = async () => { + await setBackendUrl('http://localhost:3001'); + router.replace('/(auth)/sign-in'); + }; + + if (loading) { + return ( + + Loading... + + ); + } + + return ( + + + 🔧 Connect to Backend + Enter the URL of your Bagsy API server + + + + + Save & Continue + + + + Use Localhost (3001) + + + + + 💡 For local dev: http://localhost:3001{'\n'} + For production: https://your-api-domain.com + + + + + ); +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#004E89', + justifyContent: 'center', + alignItems: 'center', + padding: 20, + }, + loadingText: { + color: '#fff', + fontSize: 18, + }, + card: { + backgroundColor: '#fff', + borderRadius: 16, + padding: 24, + width: '100%', + maxWidth: 400, + }, + title: { + fontSize: 24, + fontWeight: 'bold', + color: '#FF6B35', + marginBottom: 8, + }, + subtitle: { + fontSize: 14, + color: '#666', + marginBottom: 24, + }, + input: { + borderWidth: 1, + borderColor: '#ddd', + borderRadius: 8, + padding: 12, + fontSize: 16, + marginBottom: 16, + color: '#333', + }, + button: { + backgroundColor: '#FF6B35', + borderRadius: 8, + padding: 14, + alignItems: 'center', + marginBottom: 12, + }, + buttonText: { + color: '#fff', + fontSize: 16, + fontWeight: '600', + }, + secondaryButton: { + backgroundColor: '#f0f0f0', + borderRadius: 8, + padding: 12, + alignItems: 'center', + marginBottom: 16, + }, + secondaryButtonText: { + color: '#666', + fontSize: 14, + }, + hint: { + padding: 12, + backgroundColor: '#f9f9f9', + borderRadius: 8, + }, + hintText: { + fontSize: 12, + color: '#888', + }, +}); diff --git a/apps/mobile/app/_layout.tsx b/apps/mobile/app/_layout.tsx index 2169846..92d97f4 100644 --- a/apps/mobile/app/_layout.tsx +++ b/apps/mobile/app/_layout.tsx @@ -1,15 +1,18 @@ -import { useEffect } from 'react'; +import { useEffect, useState } from 'react'; import { Stack } from 'expo-router'; import { useAuthStore } from '@/store/auth'; import { useWebSocket } from '@/hooks/useWebSocket'; import { authClient } from '@/lib/auth-client'; +import { getBackendUrl } from '@/lib/api'; /** * Root layout with auth guard - * Routes to auth stack if not authenticated, app stack otherwise + * Routes to backend-setup if not configured, then to auth stack or app stack */ export default function RootLayout() { const { user, setUser, setIsLoading } = useAuthStore(); + const [backendConfigured, setBackendConfigured] = useState(false); + const [checked, setChecked] = useState(false); // Initialize WebSocket connection when authenticated useWebSocket(); @@ -32,16 +35,31 @@ export default function RootLayout() { } } + // Check if backend URL is configured + async function checkBackend() { + const url = await getBackendUrl(); + setBackendConfigured(!!url); + setChecked(true); + } + + checkBackend(); checkAuth(); }, [setUser, setIsLoading]); + if (!checked) { + return null; // Or a loading screen + } + return ( - {user ? ( + {!backendConfigured ? ( + // Backend not configured - show setup first + + ) : user ? ( // User is authenticated - show app stack ) : ( diff --git a/apps/mobile/lib/api.ts b/apps/mobile/lib/api.ts index 7ceb190..ef4274c 100644 --- a/apps/mobile/lib/api.ts +++ b/apps/mobile/lib/api.ts @@ -1,3 +1,36 @@ +import * as SecureStore from 'expo-secure-store'; + +const BACKEND_URL_KEY = 'backend_url'; +const DEFAULT_URL = 'http://localhost:3001'; + +// Get stored or default backend URL +export async function getBackendUrl(): Promise { + try { + const stored = await SecureStore.getItemAsync(BACKEND_URL_KEY); + return stored || DEFAULT_URL; + } catch { + return DEFAULT_URL; + } +} + +// Set the backend URL +export async function setBackendUrl(url: string): Promise { + try { + await SecureStore.setItemAsync(BACKEND_URL_KEY, url); + } catch (e) { + console.error('Failed to save backend URL:', e); + } +} + +// Clear stored backend URL (reset to default) +export async function clearBackendUrl(): Promise { + try { + await SecureStore.deleteItemAsync(BACKEND_URL_KEY); + } catch { + // Ignore errors + } +} + import { authClient } from './auth-client'; /** @@ -5,7 +38,7 @@ import { authClient } from './auth-client'; * Automatically attaches auth cookies to every request */ export async function apiFetch(endpoint: string, options: RequestInit = {}): Promise { - const baseURL = process.env.EXPO_PUBLIC_API_URL || 'http://localhost:3001'; + const baseURL = await getBackendUrl(); const url = `${baseURL}${endpoint}`; // Get auth cookies @@ -23,7 +56,7 @@ export async function apiFetch(endpoint: string, options: RequestInit = {}): const response = await fetch(url, { ...options, headers, - credentials: 'omit', // Prevent conflicts with manually set Cookie header + credentials: 'omit', }); if (!response.ok) {