bagsy/apps/mobile/app/(auth)/backend-setup.tsx
bananabas ba6aeb6c7c 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
2026-03-28 08:39:00 +00:00

157 lines
3.7 KiB
TypeScript

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