feat: add configurable backend URL with UI prompt
- App now prompts for backend URL on first launch - Stores in SecureStore for persistence - Default: http://localhost:3001 - Can be changed in app settings - Backend setup screen at /app/(auth)/backend-setup.tsx
This commit is contained in:
parent
bebfb94e0e
commit
ba6aeb6c7c
3 changed files with 213 additions and 5 deletions
157
apps/mobile/app/(auth)/backend-setup.tsx
Normal file
157
apps/mobile/app/(auth)/backend-setup.tsx
Normal file
|
|
@ -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 (
|
||||
<View style={styles.container}>
|
||||
<Text style={styles.loadingText}>Loading...</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<View style={styles.card}>
|
||||
<Text style={styles.title}>🔧 Connect to Backend</Text>
|
||||
<Text style={styles.subtitle}>Enter the URL of your Bagsy API server</Text>
|
||||
|
||||
<TextInput
|
||||
style={styles.input}
|
||||
value={url}
|
||||
onChangeText={setUrl}
|
||||
placeholder="http://localhost:3001"
|
||||
placeholderTextColor="#666"
|
||||
autoCapitalize="none"
|
||||
autoCorrect={false}
|
||||
keyboardType="url"
|
||||
/>
|
||||
|
||||
<TouchableOpacity style={styles.button} onPress={handleSave}>
|
||||
<Text style={styles.buttonText}>Save & Continue</Text>
|
||||
</TouchableOpacity>
|
||||
|
||||
<TouchableOpacity style={styles.secondaryButton} onPress={useDefault}>
|
||||
<Text style={styles.secondaryButtonText}>Use Localhost (3001)</Text>
|
||||
</TouchableOpacity>
|
||||
|
||||
<View style={styles.hint}>
|
||||
<Text style={styles.hintText}>
|
||||
💡 For local dev: http://localhost:3001{'\n'}
|
||||
For production: https://your-api-domain.com
|
||||
</Text>
|
||||
</View>
|
||||
</View>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
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',
|
||||
},
|
||||
});
|
||||
|
|
@ -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 (
|
||||
<Stack
|
||||
screenOptions={{
|
||||
headerShown: false,
|
||||
}}
|
||||
>
|
||||
{user ? (
|
||||
{!backendConfigured ? (
|
||||
// Backend not configured - show setup first
|
||||
<Stack.Screen name="(auth)/backend-setup" />
|
||||
) : user ? (
|
||||
// User is authenticated - show app stack
|
||||
<Stack.Screen name="(app)" />
|
||||
) : (
|
||||
|
|
|
|||
|
|
@ -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<string> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
|
||||
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<T>(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) {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue