-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAppNavigator.js
More file actions
150 lines (143 loc) · 4.96 KB
/
AppNavigator.js
File metadata and controls
150 lines (143 loc) · 4.96 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { NavigationContainer, DefaultTheme } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import { useState, useEffect } from 'react';
import Ionicons from '@expo/vector-icons/Ionicons';
import { StatusBar } from 'react-native';
import HomeButtonController from './components/buttons/HomeButtonController';
import LoadingOverlay from './components/loadings/LoadingOverlay';
import { getMyInfo } from './apis/MyPageApi';
import { useAuthStore } from './stores/authStore';
// 로그인 전 페이지
import WelcomePage from './pages/WelcomePage';
import LoginPage from './pages/LoginPage';
import SignUpPage from './pages/SignUpPage';
import SignUpVerificationPage from './pages/SignUpVerificationPage';
// 로그인 후 페이지
import MainPage from './pages/MainPage';
import MyPage from './pages/MyPage';
import ChangePasswordPage from './pages/ChangePasswordPage';
import AccessListPage from './pages/AccessListPage';
import MyAccessListPage from './pages/MyAccessListPage';
import AccessRequestPage from './pages/AccessRequestPage';
import AccessRequestRolePage from './pages/AccessRequestRolePage';
import { colors } from './constants/colors';
const Stack = createStackNavigator();
export default function AppNavigator() {
const {
isLoggedIn,
setIsLoggedIn,
accessToken,
setLoading,
loading,
setAccessToken,
clearAccessToken,
} = useAuthStore();
const [navState, setNavState] = useState(null);
// 앱 시작 시 토큰 유효성 확인
useEffect(() => {
const checkToken = async () => {
setLoading(true);
try {
await new Promise((resolve) => setTimeout(resolve, 1000));
if (accessToken) {
// 회원 정보 조회로 토큰 유효성 검증
try {
await getMyInfo();
setAccessToken(accessToken); // 토큰 유효
} catch (err) {
//에러 발생 시
clearAccessToken();
}
} else {
setIsLoggedIn(false);
}
} catch (e) {
setIsLoggedIn(false);
} finally {
setLoading(false);
}
};
checkToken();
}, []);
const navTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: colors.white,
},
};
return (
<NavigationContainer onStateChange={setNavState} theme={navTheme}>
<LoadingOverlay visible={loading} /*로딩*/ />
<StatusBar hidden />
<HomeButtonController state={navState} />
<Stack.Navigator
screenOptions={{
headerStyle: { backgroundColor: colors.secondary, height: 120 },
headerTintColor: colors.white,
headerTitleStyle: { fontWeight: '600', fontSize: 26 },
headerTitleAlign: 'center',
animationEnabled: false,
gestureEnabled: true,
headerBackImage: () => <Ionicons name="chevron-back" size={24} color={colors.white} />,
headerBackTitle: '',
}}
>
{isLoggedIn ? (
<>
<Stack.Screen
name="MainPage"
component={MainPage}
options={{ headerShown: false, title: '홈' }}
/>
<Stack.Screen name="MyPage" component={MyPage} options={{ headerShown: false }} />
<Stack.Screen
name="ChangePasswordPage"
component={ChangePasswordPage}
options={{ headerShown: false }}
/>
<Stack.Screen
name="AccessListPage"
component={AccessListPage}
options={{ title: '출입 권한' }}
/>
<Stack.Screen
name="MyAccessListPage"
component={MyAccessListPage}
options={{ title: '권한 목록 조회' }}
/>
<Stack.Screen
name="AccessRequestPage"
component={AccessRequestPage}
options={{ title: '출입 권한 신청' }}
/>
<Stack.Screen
name="AccessRequestRolePage"
component={AccessRequestRolePage}
options={{ title: '출입 권한 신청' }}
/>
</>
) : (
<>
<Stack.Screen
name="WelcomePage"
component={WelcomePage}
options={{ headerShown: false }}
/>
<Stack.Screen name="LoginPage" component={LoginPage} options={{ headerShown: false }} />
<Stack.Screen
name="SignUpPage"
component={SignUpPage}
options={{ headerShown: false }}
/>
<Stack.Screen
name="SignUpVerificationPage"
component={SignUpVerificationPage}
options={{ headerShown: false }}
/>
</>
)}
</Stack.Navigator>
</NavigationContainer>
);
}