-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
83 lines (76 loc) · 2.47 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
76
77
78
79
80
81
82
83
/* eslint-disable no-unused-vars */
/* eslint-disable no-var */
/* eslint-disable vars-on-top */
/* eslint-disable global-require */
import React, { useState, useEffect } from 'react';
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import { initializeAuth } from 'firebase/auth';
import { getReactNativePersistence } from 'firebase/auth/react-native';
import 'firebase/compat/database';
import * as Font from 'expo-font';
import * as Notifications from 'expo-notifications';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-toast-message';
import ApiKeys from './ApiKeys';
import { Provider } from './Context';
import NavContainer from './NavContainer';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
export default function App() {
const [isLoadingComplete, setIsLoadingComplete] = useState(false);
const [isAuthenticationReady, setIsAuthenticationReady] = useState(false);
const [user, setUser] = useState(null);
const onAuthStateChanged = (u: any) => {
setIsAuthenticationReady(true);
if (!u) {
setUser(null);
}
if (u) {
const idxHarvard = u.email.indexOf('harvard.edu');
if (idxHarvard === -1 && u.email !== '[email protected]' && u.email !== '[email protected]' && u.email !== '[email protected]') {
firebase.auth().signOut();
Toast.show({
type: 'error',
text1: 'We don\'t recognize your school email.',
text2: 'It\'s possible that Wado isn\'t available at your school yet.',
});
} else {
setUser(u);
}
}
};
if (!firebase.apps.length) {
const app = firebase.initializeApp(ApiKeys.FirebaseConfig);
initializeAuth(app, {
persistence: getReactNativePersistence(AsyncStorage),
});
}
firebase.auth().onAuthStateChanged(onAuthStateChanged);
const loadFonts = async () => {
await Font.loadAsync({
// Load a font `Montserrat` from a static resource
Montserrat: require('./assets/Montserrat-Regular.ttf'),
MontserratBold: require('./assets/Montserrat-Bold.ttf'),
});
setIsLoadingComplete(true);
};
useEffect(() => {
loadFonts();
}, []);
if (!isLoadingComplete || !(isAuthenticationReady || user)) {
return (
null
);
}
return (
<Provider>
<NavContainer user={user}/>
</Provider>
);
}