-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
211 lines (200 loc) · 5.97 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, {Component} from 'react';
import {Alert} from 'react-native';
import * as eva from '@eva-design/eva';
import {ApplicationProvider, IconRegistry} from '@ui-kitten/components';
import {EvaIconsPack} from '@ui-kitten/eva-icons';
import LoginNavigator from './Screens/Navigators/LoginNavigator';
import MainNavigator from './Screens/Navigators/MainNavigator';
import {StoreData, GetData} from './Utils/AsyncStorage';
import theme from './src/themes/theme';
import mapping from './src/assets/mapping';
import {client} from './back-end/OurApi';
import {gql} from '@apollo/client';
import auth from '@react-native-firebase/auth';
/**GLOBALS START*/
global.email = '';
global.user_id = '';
global.tckn = '';
global.real_name = '';
global.password = '';
global.friendsAdded = false;
global.subscriptionWarningEnabled = false;
/**GLOBALS END*/
export default class App extends Component {
constructor(props) {
super(props);
this.state = {isLoggedIn: false};
}
componentDidMount() {
this.checkCredentials();
}
/** ASYNC STORAGE START*/
clearLoginInfo() {
StoreData('email', '');
StoreData('user_id', '');
StoreData('real_name', '');
StoreData('password', '');
global.email = '';
global.user_id = '';
global.tckn = '';
global.real_name = '';
global.password = '';
this.setState({isLoggedIn: false});
}
saveLoginInfo(user) {
StoreData('email', user.email);
StoreData('user_id', user.user_id);
StoreData('real_name', user.name);
StoreData('password', user.password);
global.email = user.email;
global.user_id = user.user_id;
global.tckn = user.tckn;
global.real_name = user.real_name;
global.password = user.password;
this.setState({isLoggedIn: true});
}
/** ASYNC STORAGE END*/
// Login from async storage
async checkCredentials() {
let email = await GetData('email');
let password = await GetData('password');
if (
email !== null &&
email !== '' &&
password !== null &&
password !== ''
) {
let user = {email: email, password: password};
await auth()
.signInWithEmailAndPassword(email, password)
.then(res => {
console.log('User account signed in!');
this.saveLoginInfo(user);
})
.catch(error => {
if (error.code === 'auth/email-already-in-use') {
Alert.alert('Email already in use, please try sign-in.');
} else if (error.code === 'auth/invalid-email') {
Alert.alert('Email address is invalid');
} else {
Alert.alert('A problem occurred.');
}
console.error(error);
});
}
}
async logInUserWithPassword(email, password, callback) {
let user = {email: email, password: password};
await auth()
.signInWithEmailAndPassword(email, password)
.then(res => {
console.log('User account signed in!');
client
.query({
query: gql`
query MyQuery($email: String, $password: String) {
users(
where: {
email: {_eq: $email}
_and: {password: {_eq: $password}}
}
) {
email
real_name
password
tckn
user_id
}
}
`,
variables: user,
})
.then(result => {
console.log(result);
if (result.data.users.length === 1) {
let user = result.data.users[0];
this.saveLoginInfo(user);
callback();
} else {
Alert.alert('Email veya sifre yanlis.');
callback();
}
})
.catch(result => {
console.log(result);
Alert.alert('Bir hata olustu.');
callback();
});
})
.catch(error => {
if (error.code === 'auth/email-already-in-use') {
Alert.alert('Email already in use, please try sign-in.');
} else if (error.code === 'auth/invalid-email') {
Alert.alert('Email address is invalid');
} else {
Alert.alert('A problem occurred.');
}
console.log(error);
});
}
async register(name, email, password, callback) {
// Firebase part
await auth()
.createUserWithEmailAndPassword(email, password)
.then(res => {
console.log('User account created & signed in!');
let user = {
real_name: name,
email: email,
password: password,
user_id: res.user.uid,
};
console.log(res);
this.saveLoginInfo(user);
callback();
})
.catch(error => {
if (error.code === 'auth/email-already-in-use') {
Alert.alert('Email already in use, please try sign-in.');
} else if (error.code === 'auth/invalid-email') {
Alert.alert('Email address is invalid');
} else {
Alert.alert('An undefined problem occurred.');
}
callback();
console.log(error);
});
}
logout = () => {
this.clearLoginInfo();
this.setState({isLoggedIn: false});
};
render() {
return (
<>
<IconRegistry icons={EvaIconsPack} />
<ApplicationProvider
{...eva}
theme={theme.light}
customMapping={mapping}>
{this.state.isLoggedIn ? (
<MainNavigator
mainFunctions={{
logout: () => this.logout(),
}}
/>
) : (
<LoginNavigator
mainFunctions={{
logInUser: (email, password, callback) =>
this.logInUserWithPassword(email, password, callback),
register: (name, email, password, callback) =>
this.register(name, email, password, callback),
}}
/>
)}
</ApplicationProvider>
</>
);
}
}