Skip to content

Commit 59c54f3

Browse files
committed
type decleration for auth
1 parent 9c9b29c commit 59c54f3

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

app/(tabs)/_layout.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
import { Stack } from 'expo-router';
1+
import { Redirect, Stack } from 'expo-router';
2+
3+
import { useAuth } from '~/context/AuthContext';
24

35
export default function TabLayout() {
6+
const { user } = useAuth();
7+
8+
if (!user) {
9+
return <Redirect href="/(auth)/auth" />;
10+
}
411
return (
512
<Stack screenOptions={{ headerStyle: { backgroundColor: 'orange' } }}>
613
<Stack.Screen name="index" options={{ title: 'ChefBuddy' }} />

app/_layout.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function RootLayout() {
88
<AuthContextProvider>
99
<Stack>
1010
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
11+
<Stack.Screen name="(auth)" options={{ headerShown: false }} />
1112
</Stack>
1213
</AuthContextProvider>
1314
);

context/AuthContext.tsx

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { Session } from '@supabase/supabase-js';
1+
import { Session, User } from '@supabase/supabase-js';
22
import { createContext, PropsWithChildren, useContext, useEffect, useState } from 'react';
33
import { ActivityIndicator } from 'react-native';
44

55
import { supabase } from '~/utils/supabase';
66

7-
const AuthContext = createContext({});
7+
type AuthContextType = {
8+
session: Session | null;
9+
user: User | null;
10+
};
11+
const AuthContext = createContext<AuthContextType | undefined>(undefined);
12+
813
export default function AuthContextProvider({ children }: PropsWithChildren) {
914
const [session, setSession] = useState<Session | null>(null);
1015
const [isReady, setIsReady] = useState(false);
@@ -19,14 +24,16 @@ export default function AuthContextProvider({ children }: PropsWithChildren) {
1924
setSession(session);
2025
});
2126

22-
return () => subscription?.unsubscribe();
27+
return () => subscription?.subscription.unsubscribe();
2328
}, []);
2429

2530
if (!isReady) {
2631
return <ActivityIndicator />;
2732
}
2833
return (
29-
<AuthContext.Provider value={{ session, user: session?.user }}>{children}</AuthContext.Provider>
34+
<AuthContext.Provider value={{ session, user: session?.user || null }}>
35+
{children}
36+
</AuthContext.Provider>
3037
);
3138
}
3239

0 commit comments

Comments
 (0)