-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
79 lines (69 loc) · 2.23 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import 'react-native-url-polyfill/auto';
import React, { createContext, useState, useEffect } from 'react';
import { supabase } from './src/lib/supabase';
import { Session } from '@supabase/supabase-js';
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import HomeScreen from './src/screens/HomeScreen';
import LoginScreen from './src/screens/LoginScreen';
import SplashScreen from 'react-native-splash-screen';
// 기본 네비게이션 타입
export type RootStackParamList = {
Home: undefined;
Login: undefined;
};
const Stack = createStackNavigator<RootStackParamList>();
// 세션 컨텍스트
export const SessionContext = createContext<{
session: Session | null;
setSession: (session: Session | null) => void;
}>({
session: null,
setSession: () => {},
});
export default function App(): React.JSX.Element {
const [session, setSession] = useState<Session | null>(null);
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const initializeApp = async () => {
try {
const { data: { session } } = await supabase.auth.getSession();
setSession(session);
} catch (error) {
console.error('Session load error:', error);
} finally {
setIsLoading(false);
}
};
// 스플래시 스크린 처리
Promise.all([
initializeApp(),
new Promise(resolve => setTimeout(resolve, 1000))
]).then(() => {
SplashScreen.hide();
});
// 인증 상태 변경 리스너
const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => {
setSession(session);
});
return () => {
subscription.unsubscribe();
};
}, []);
if (isLoading) {
return <></>; // 또는 로딩 컴포넌트
}
return (
<SessionContext.Provider value={{ session, setSession }}>
<NavigationContainer>
<Stack.Navigator screenOptions={{ headerShown: false }}>
{session ? (
<Stack.Screen name="Home" component={HomeScreen} />
) : (
<Stack.Screen name="Login" component={LoginScreen} />
)}
</Stack.Navigator>
</NavigationContainer>
</SessionContext.Provider>
);
}