App.js
2.33 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
import React, {useState} from 'react';
import {View} from 'react-native';
import {AppLoading} from 'expo';
import * as SecureStore from 'expo-secure-store';
import AsyncStorage from '@react-native-community/async-storage';
import axios from "axios";
import env from './env';
import {StatusBar} from 'expo-status-bar';
import {Provider} from 'react-redux';
import store from "./store";
import {AuthProvider} from "./AuthContext";
import RootNavigation from "./navigations/RootNavigation";
export default function App() {
const [isAppLoading, setIsAppLoading] = useState(true);
const [user, setUser] = useState(null);
const loadAssets = async () => {
return new Promise(async (resolve, reject) => {
try {
let cookie;
if (SecureStore.isAvailableAsync()) {
cookie = await SecureStore.getItemAsync('cookie');
} else {
cookie = await AsyncStorage.getItem('cookie');
}
if (cookie) {
axios.defaults.headers.Cookie = cookie;
const res = await axios.get(`${env.HTTP_OR_HTTPS}://${env.HOST}:${env.PORT}/user/loadMe`, {
withCredentials: true
});
const {user} = res.data;
setUser(user);
}
return resolve();
} catch (e) {
console.error(e);
return reject();
}
})
};
const onFinish = async () => {
setIsAppLoading(false);
};
const onError = (err) => {
console.error(err);
};
return (
<>
{isAppLoading
?
<AppLoading
startAsync={loadAssets}
onFinish={onFinish}
onError={onError}/>
:
<View style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'center'
}}>
<Provider store={store}>
<AuthProvider user={user}>
<RootNavigation/>
</AuthProvider>
</Provider>
<StatusBar/>
</View>
}
</>
);
}