fix: handle login expirary

This commit is contained in:
2026-03-10 12:12:37 -04:00
Unverified
parent 40fc5c1de6
commit 5c7031725c
5 changed files with 121 additions and 30 deletions

View File

@@ -1,9 +1,34 @@
import { Ionicons } from '@expo/vector-icons';
import { Stack, useRouter, useSegments } from 'expo-router';
import { useEffect } from 'react';
import { Text, TouchableOpacity } from 'react-native';
import { useColorScheme } from '../hooks/use-color-scheme';
import { AuthProvider, useAuth } from '../src/context/AuthContext';
function AuthRedirect() {
const { isAuthenticated, isLoading } = useAuth();
const router = useRouter();
const segments = useSegments();
useEffect(() => {
if (isLoading) {
return;
}
const isLoginRoute = segments[0] === 'login';
if (!isAuthenticated && !isLoginRoute) {
router.replace('/login');
}
if (isAuthenticated && isLoginRoute) {
router.replace('/(tabs)');
}
}, [isAuthenticated, isLoading, router, segments]);
return null;
}
function DevicesHeader() {
const router = useRouter();
const isDark = useColorScheme() === 'dark';
@@ -45,6 +70,7 @@ export default function RootLayout() {
return (
<AuthProvider>
<AuthRedirect />
<Stack>
{/* Root index that performs auth redirect (app/index.tsx) */}
<Stack.Screen name="index" options={{ headerShown: false }} />