morethanadiagnosis-hub/mobile/app/_layout.tsx
Claude eb04163b3b
feat: implement authentication system for mobile app
- Add API integration layer with axios and token management
- Create Zustand auth store for state management
- Implement login screen with validation
- Implement signup screen with password strength indicator
- Implement forgot password flow with multi-step UI
- Update root layout with auth state protection
- Integrate profile screen with auth store
- Install AsyncStorage and expo-secure-store dependencies
2025-11-18 19:32:16 +00:00

73 lines
1.8 KiB
TypeScript

import { Stack, useRouter, useSegments } from 'expo-router';
import { StatusBar } from 'expo-status-bar';
import { useEffect } from 'react';
import { View, ActivityIndicator, StyleSheet } from 'react-native';
import { useAuthStore } from './lib/auth-store';
export const unstable_settings = {
initialRouteName: '(tabs)',
};
// Auth protection hook
function useProtectedRoute() {
const { isAuthenticated, isInitialized } = useAuthStore();
const segments = useSegments();
const router = useRouter();
useEffect(() => {
if (!isInitialized) {
return;
}
const inAuthGroup = segments[0] === '(auth)';
if (!isAuthenticated && !inAuthGroup) {
// Redirect to login if not authenticated and not in auth group
router.replace('/(auth)/login');
} else if (isAuthenticated && inAuthGroup) {
// Redirect to tabs if authenticated and in auth group
router.replace('/(tabs)');
}
}, [isAuthenticated, isInitialized, segments]);
}
export default function RootLayout() {
const { initialize, isInitialized } = useAuthStore();
// Initialize auth state on app start
useEffect(() => {
initialize();
}, []);
// Use protected route hook
useProtectedRoute();
// Show loading screen while initializing
if (!isInitialized) {
return (
<View style={styles.loadingContainer}>
<StatusBar style="dark" />
<ActivityIndicator size="large" color="#007AFF" />
</View>
);
}
return (
<>
<StatusBar style="dark" />
<Stack>
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
<Stack.Screen name="(auth)" options={{ headerShown: false }} />
</Stack>
</>
);
}
const styles = StyleSheet.create({
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#FFFFFF',
},
});