-
Notifications
You must be signed in to change notification settings - Fork 60
/
App.tsx
75 lines (63 loc) · 2.21 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
import { StatusBar } from 'expo-status-bar';
import React, {useEffect} from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import Amplify, {
Auth,
API,
graphqlOperation
} from 'aws-amplify'
import { withAuthenticator } from 'aws-amplify-react-native'
import useCachedResources from './hooks/useCachedResources';
import useColorScheme from './hooks/useColorScheme';
import Navigation from './navigation';
import config from './aws-exports'
import { getUser } from './src/graphql/queries';
import { createUser } from './src/graphql/mutations';
Amplify.configure(config)
function App() {
const isLoadingComplete = useCachedResources();
const colorScheme = useColorScheme();
const getRandomImage = () => {
return 'https://scontent.fkiv3-1.fna.fbcdn.net/v/t31.0-8/s960x960/22256588_1932617800312085_5686197942193420542_o.jpg?_nc_cat=110&_nc_sid=85a577&_nc_ohc=svjjE7DUkc0AX9yjcdC&_nc_ht=scontent.fkiv3-1.fna&tp=7&oh=1df4116c73c45a32ebad070704ca3333&oe=5F6ECD77'
}
const saveUserToDB = async (user) => {
console.log(user);
await API.graphql(graphqlOperation(createUser, { input: user }))
}
useEffect(() => {
const updateUser = async () => {
// Get current authenticated user
const userInfo = await Auth.currentAuthenticatedUser({ bypassCache: true });
if(userInfo) {
// Check if user already exists in database
const userData = await API.graphql(graphqlOperation(getUser, { id: userInfo.attributes.sub }));
console.log(userData)
if(!userData.data.getUser) {
const user = {
id: userInfo.attributes.sub,
username: userInfo.username,
name: userInfo.username,
email: userInfo.attributes.email,
image: getRandomImage(),
}
await saveUserToDB(user);
} else {
console.log('User already exists');
}
}
// If it doesn't, create the user in the database
}
updateUser();
}, [])
if (!isLoadingComplete) {
return null;
} else {
return (
<SafeAreaProvider>
<Navigation colorScheme={colorScheme} />
<StatusBar />
</SafeAreaProvider>
);
}
}
export default withAuthenticator(App);