-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
191 lines (168 loc) · 7.54 KB
/
App.js
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
import { useFonts } from 'expo-font';
import { NavigationContainer } from '@react-navigation/native'
import { createStackNavigator, CardStyleInterpolators } from '@react-navigation/stack'
import OnBoarding from "./Components/OnBoarding/OnBoarding";
import { FirstPage } from './Components/Register/FirstPage';
import { SecondPage } from './Components/Register/SecondPage'
import EmailVerification from './Components/Register/EmailVerification/EmailVerification';
import Login from './Components/Login/Login'
import AsyncStorage from '@react-native-async-storage/async-storage';
import { init } from './Localization';
import { Easing, AppRegistry } from 'react-native';
import MenuBar from './Components/MenuBar/MenuBar';
import ForgotPasswordFirst from './Components/ForgetPassword/ForgetPasswordFirst';
import ForgotPasswordSecond from './Components/ForgetPassword/ForgetPasswordSecond';
import ForgotPasswordThird from './Components/ForgetPassword/ForgetPasswordThird';
import { StatusBar } from 'expo-status-bar';
import { goPage, setNavigationRef } from './Helpers/Navigator';
import { useEffect, useState } from 'react';
import Backend from './Helpers/Backend';
import MainContext from './Components/Context/MainContext';
// import the screen
const Stack = createStackNavigator();
export default function App() {
// initialize the language
init();
// get the font from the local fonts
let [fontsLoaded] = useFonts({
'CapitalisTypOasis': require('./assets/fonts/CapitalisTypOasis.ttf'),
'Montserrat-Medium': require('./assets/fonts/Montserrat-Medium.ttf'),
'Montserrat-Bold': require('./assets/fonts/Montserrat-Bold.ttf'),
'Montserrat-Regular': require('./assets/fonts/Montserrat-Regular.ttf'),
'Montserrat-SemiBold': require('./assets/fonts/Montserrat-SemiBold.ttf'),
'Montserrat-Thin': require('./assets/fonts/Montserrat-Thin.ttf'),
'Montserrat-Light': require('./assets/fonts/Montserrat-Light.ttf'),
'Montserrat-Black': require('./assets/fonts/Montserrat-Black.ttf'),
'Poppins-Regular': require('./assets/fonts/Poppins-Regular.ttf'),
'Poppins-Bold': require('./assets/fonts/Poppins-Bold.ttf'),
'Poppins-Medium': require('./assets/fonts/Poppins-Medium.ttf'),
'Poppins-SemiBold': require('./assets/fonts/Poppins-SemiBold.ttf'),
'Poppins-Thin': require('./assets/fonts/Poppins-Thin.ttf'),
'Poppins-Light': require('./assets/fonts/Poppins-Light.ttf'),
'Poppins-Black': require('./assets/fonts/Poppins-Black.ttf'),
});
// animation config
const timingConfig = {
animation: 'timing',
config: {
duration: 500,
easing: Easing.linear,
},
};
// get the access token from the local storage
const [accessToken, setAccessToken] = useState(null);
const [refreshToken, setRefreshToken] = useState(null);
const [isTokensChecked, setIsTokensChecked] = useState(false);
const getAccessToken = async () => {
try {
// refresh the token before getting it
await refresh_the_token();
// get the access token and refresh token from the local storage
const access = await AsyncStorage.getItem('accessToken').then((accessToken) => {
if (accessToken !== null) {
return accessToken;
} else {
return null;
}
});
const refresh = await AsyncStorage.getItem('refreshToken').then((refreshToken) => {
if (refreshToken !== null) {
return refreshToken;
} else {
return null;
}
});
setAccessToken(access);
setRefreshToken(refresh);
setIsTokensChecked(true);
} catch (e) {
console.log(e);
}
};
// get the access token from the local storage
useEffect(() => {
getAccessToken();
}, []);
// Function to refresh token
const refresh_the_token = async () => {
try {
// get the access token first
const refreshToken = await AsyncStorage.getItem('refreshToken').then(response => response);
// if the access token is null, return
if (!refreshToken)
return;
// if the access token is null, return
const { statusCode, data } = await Backend.refresh_the_token(refreshToken).then(response => response);
// if the response is not null, set the access token
if (Backend.isSuccessfulRequest(statusCode)) {
await AsyncStorage.setItem('accessToken', data.access);
await AsyncStorage.setItem('refreshToken', data.refresh);
setAccessToken(data.access);
setRefreshToken(data.refresh);
console.log(data.refresh);
} else {
await AsyncStorage.removeItem('accessToken');
await AsyncStorage.removeItem('refreshToken');
setAccessToken(null);
setRefreshToken(null);
goPage('login');
}
} catch (e) {
console.log('Error refereshing:', e);
}
};
// get referesh token
useEffect(() => {
// Refresh token every 4 minutes (240,000 milliseconds)
const refreshInterval = setInterval(refresh_the_token, 240000);
// Clean up the interval on component unmount
return () => {
clearInterval(refreshInterval);
};
}, []);
// if the access token is null, return the login page
if (!isTokensChecked || !fontsLoaded)
return null
// clear async storage
// AsyncStorage.clear();
// if the font loaded, return the components
return (
<MainContext >
<StatusBar
backgroundColor={'transparent'}
barStyle='light-content'
style='light'
translucent={true}
/>
<NavigationContainer
ref={navigationRef => {
setNavigationRef(navigationRef);
}}
>
<Stack.Navigator
screenOptions={{
header: () => null,
cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
transitionSpec: {
open: timingConfig,
close: timingConfig,
},
headerStatusBarHeight: 0,
}}
initialRouteName={accessToken && refreshToken ? "menuBar" : "onBoarding"}
>
<Stack.Screen name="onBoarding" component={OnBoarding} />
<Stack.Screen name="firstPage" component={FirstPage} />
<Stack.Screen name="secondPage" component={SecondPage} />
<Stack.Screen name="login" component={Login} />
<Stack.Screen name="forgotPasswordFirst" component={ForgotPasswordFirst} />
<Stack.Screen name="forgotPasswordSecond" component={ForgotPasswordSecond} />
<Stack.Screen name="forgotPasswordThird" component={ForgotPasswordThird} />
<Stack.Screen name="menuBar" component={MenuBar} />
<Stack.Screen name="emailVerification" component={EmailVerification} />
</Stack.Navigator>
</NavigationContainer>
</MainContext>
);
}
AppRegistry.registerComponent('App', () => App);