refactor: add AuthGuard component and fix API client empty response handling

- Add reusable AuthGuard component for protected routes
- Move auth logic from index.tsx to (tabs)/_layout.tsx via AuthGuard
- Fix ApiClient to handle empty responses (204 No Content)
- Use useFocusEffect in chat.tsx to load messages after auth is ready
- Extract getDateKey() helper function in calendar.tsx
This commit is contained in:
2026-01-25 13:03:17 +01:00
parent 43d40b46d7
commit a42e2a7c1c
7 changed files with 112 additions and 65 deletions

View File

@@ -0,0 +1,46 @@
import { useEffect, ReactNode } from "react";
import { View, ActivityIndicator } from "react-native";
import { Redirect } from "expo-router";
import { useAuthStore } from "../stores";
import { useThemeStore } from "../stores/ThemeStore";
type AuthGuardProps = {
children: ReactNode;
};
/**
* Wraps content that requires authentication.
* - Loads stored user on mount
* - Shows loading indicator while checking auth state
* - Redirects to login if not authenticated
* - Renders children if authenticated
*/
export const AuthGuard = ({ children }: AuthGuardProps) => {
const { theme } = useThemeStore();
const { isAuthenticated, isLoading, loadStoredUser } = useAuthStore();
useEffect(() => {
loadStoredUser();
}, [loadStoredUser]);
if (isLoading) {
return (
<View
style={{
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: theme.primeBg,
}}
>
<ActivityIndicator size="large" color={theme.chatBot} />
</View>
);
}
if (!isAuthenticated) {
return <Redirect href="/login" />;
}
return <>{children}</>;
};