-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
110 lines (88 loc) · 2.85 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
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import { StackNavigator } from 'react-navigation'
import Main from './src/screens/Main'
import LogInScreen from './src/screens/LogInScreen'
import { YellowBox } from 'react-native';
import LoginBackground from "./src/components/LoginBackground";
import User from "./src/actors/User";
import Database from './src/Database';
//Ignore those annoying deprecated warnings.
YellowBox.ignoreWarnings([
'Warning: componentWillMount is deprecated',
'Warning: componentWillReceiveProps is deprecated',
'Warning: componentWillUpdate is deprecated'
]);
/**
* Initialize Database
*/
Database.initialize();
/**
* Main class in project. It serves as the entry point of the app.Authentication is performed here and the user is sent to Login Screen if not authenticated or the Main Stack otherwise.
*/
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = ({
loggedIn: false,
loaded: false
});
}
componentDidMount(){
// Called every time database authentication is changed (login or logout)
Database.onAuthChanged((user) => {
if (user != null) {
Database.getUser(user.uid, (dbUser) => {
// If user doesn't exist, we create a reference in database and retrieve the new user.
if (dbUser == null)
this.createNewUser(user);
// Otherwise, we initialize a local user object for current user.
else
User.currentUser = new User(dbUser, !User.newUserFromFB);
//NEEDED TO NOT GET CAUGHT IN BACKGROUND SCREEN
this.setState({
loaded: true,
loggedIn: true,
});
});
}
else {
//DO NOT CHANGE THIS NEEDED FOR LOGOUT
this.setState({
loaded: true,
loggedIn: false,
});
User.currentUser = null;
}
});
}
/**
* If user does not exist create it on Firebase
* @param fbUser
*/
createNewUser(fbUser) {
let newUser = new User(fbUser, User.newUserFromFB);
User.currentUser = newUser;
Database.createUser(newUser);
}
render() {
if (!this.state.loaded) {
return (
<LoginBackground/>
);
}
else {
if (this.state.loggedIn ) {
return (
<RootStack/>
);
}
return <LogInScreen/>
}
}
}
const RootStack = StackNavigator({
Main: {screen: Main},
}, {
initialRouteName: "Main",
});