-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppInner.tsx
192 lines (173 loc) · 5.53 KB
/
AppInner.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import * as React from 'react';
import {useEffect} from 'react';
import {NavigationContainer} from '@react-navigation/native';
import SignIn from './src/pages/SignIn';
import SignUp from './src/pages/SignUp';
import Settings from './src/pages/Settings';
import Orders from './src/pages/Orders';
import Delivery from './src/pages/Delivery';
import {createNativeStackNavigator} from '@react-navigation/native-stack';
import {createBottomTabNavigator} from '@react-navigation/bottom-tabs';
import {useSelector} from 'react-redux';
import {RootState} from './src/store/reducer';
import useSocket from './src/hooks/useSocket';
import {useAppDispatch} from './src/store';
import EncryptedStorage from 'react-native-encrypted-storage';
import axios, {AxiosError, AxiosResponse} from 'axios';
import {API_URL} from './src/config/index';
import userSlice from './src/slices/user';
import orderSlice, {Order} from './src/slices/order';
import {Alert} from 'react-native';
import usePermissions from './src/hooks/usePermissions';
import SplashScreen from 'react-native-splash-screen';
// 로그인 하지 않은 화면
export type RootStackParamList = {
SignIn: undefined;
SignUp: undefined;
};
export type LoggedInParamList = {
Orders: undefined;
Settings: undefined;
Delivery: undefined;
Complete: {orderId: string};
};
const Tab = createBottomTabNavigator();
const Stack = createNativeStackNavigator();
function AppInner() {
const dispatch = useAppDispatch();
const isLoggedIn = useSelector((state: RootState) => !!state.user.email);
const [socket, disconnect] = useSocket();
usePermissions();
useEffect(() => {
axios.interceptors.response.use(
response => {
return response;
},
async error => {
const {
config,
response: {status},
} = error;
if (status === 419) {
// console.log('interceptor config: ', config);
if (error.response.data.code === 'expired') {
const originalRequest = config;
const refreshToken = await EncryptedStorage.getItem('refreshToken');
console.log('refreshToken ', refreshToken);
// token refresh 요청
const {data} = await axios.post(
`${API_URL}/refreshToken`, // token refresh api
{},
{headers: {authorization: `Bearer ${refreshToken}`}},
);
// 새로운 토큰 저장
dispatch(userSlice.actions.setAccessToken(data.data.accessToken));
originalRequest.headers.authorization = `Bearer ${data.data.accessToken}`;
// 419로 요청 실패했던 요청 새로운 토큰으로 재요청
return axios(originalRequest);
}
}
return Promise.reject(error);
},
);
}, [dispatch]);
useEffect(() => {
const callback = (data: Order) => {
console.log(data);
dispatch(orderSlice.actions.addOrder(data));
};
if (socket && isLoggedIn) {
console.log(socket);
// 특정 이벤트 key 로 연결하고 특정 key 로 값을 받는다.
socket.emit('acceptOrder', 'hello');
socket.on('order', callback);
}
return () => {
if (socket) {
socket.off('order', callback);
}
};
}, [dispatch, isLoggedIn, socket]);
useEffect(() => {
if (!isLoggedIn) {
disconnect();
}
}, [isLoggedIn, disconnect]);
// 앱 실행 시 토큰 있으면 로그인하는 코드
useEffect(() => {
const getTokenAndRefresh = async () => {
await EncryptedStorage.removeItem('refreshToken');
try {
const token = await EncryptedStorage.getItem('refreshToken');
// console.log('getTokenAndRefresh: ', token);
if (!token) {
SplashScreen.hide();
return;
}
const response = await axios.post(
`${API_URL}/refreshToken`,
{},
{
headers: {
authorization: `Bearer ${token}`,
},
},
);
dispatch(
userSlice.actions.setUser({
name: response.data.data.name,
email: response.data.data.email,
accessToken: response.data.data.accessToken,
}),
);
} catch (error) {
const errorResponse = (error as AxiosError).response as AxiosResponse;
console.error(errorResponse);
if (errorResponse?.data.code === 'expired') {
Alert.alert('알림', '다시 로그인 해주세요.');
}
} finally {
// TODO: 스플래시 스크린 없애기
SplashScreen.hide();
}
};
getTokenAndRefresh();
}, [dispatch]);
return (
<NavigationContainer>
{isLoggedIn ? (
<Tab.Navigator>
<Tab.Screen
name="Orders"
component={Orders}
options={{title: 'Order List'}}
/>
<Tab.Screen
name="Delivery"
component={Delivery}
options={{headerShown: false, unmountOnBlur: true}}
/>
<Tab.Screen
name="Settings"
component={Settings}
options={{title: '내 정보'}}
/>
</Tab.Navigator>
) : (
<Stack.Navigator>
<Stack.Screen
name="SignIn"
component={SignIn}
options={{title: '로그인'}}
/>
<Stack.Screen
name="SignUp"
component={SignUp}
options={{title: '회원가입'}}
/>
</Stack.Navigator>
)}
</NavigationContainer>
);
}
export default AppInner;