-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
438 lines (416 loc) · 12.8 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import React, { Component } from "react";
import {
Platform,
StyleSheet,
Text,
View,
Button,
ActivityIndicator,
AsyncStorage,
AppState,
SafeAreaView,
NetInfo,
Alert,
InteractionManager
} from "react-native";
import Auth0 from "react-native-auth0";
import Login from "./src/screens/Login";
import Router from "./src/screens/Router";
import Onboarding from "./src/screens/Onboarding";
import axios from "axios";
import { devApi, prodApi } from "./config";
import TeamPicker from "./src/components/TeamPicker";
import moment from "moment";
import { Sentry } from "react-native-sentry";
Sentry.config(
"https://ea66ac6e6f1e459780ad970670698615:[email protected]/289792"
).install();
const auth0 = new Auth0({
domain: "clean-eating.auth0.com",
clientId: "F4tg24oB2Xm6VsQ5eUhISkykK4S5T8xy"
});
export default class App extends Component {
constructor(props) {
super(props);
console.log("CONSTRUCTOR: ", AppState.currentState);
this.state = {
onboardingComplete: false,
loading: true,
accessToken: null,
mongoId: null,
mongoUser: null, //TODO: probably don't need this, just need the mongoId
authId: null,
showTeamPicker: false,
loggingIn: false
};
// TODO: Use dev variable to figure out what api to use, update config file accordingly
if (__DEV__) {
// this.apiURL = 'http://localhost:4000/api';
this.apiURL = devApi;
} else {
this.apiURL = prodApi;
}
}
async componentDidMount() {
AppState.addEventListener("change", this._handleAppStateChange);
NetInfo.addEventListener("connectionChange", this.handleConnectionChange);
const loggedInUser = await AsyncStorage.multiGet([
"accessToken",
"authId",
"onboardingComplete",
"mongoId",
"refreshToken",
"tokenExpiration"
]);
console.log(loggedInUser);
if (loggedInUser[0][1]) {
let tokenExpired = moment().isSameOrAfter(
moment(parseInt(loggedInUser[5][1]))
);
if (tokenExpired) {
console.log(
"Current token expired, getting new one before getting profile"
);
await this.updateAccessToken(loggedInUser[4][1]);
this.getMongoProfile(loggedInUser[1][1], this.state.accessToken);
} else {
console.log("Current token valid, using it to get profile");
this.getMongoProfile(loggedInUser[1][1], loggedInUser[0][1]);
}
} else {
this.setState({ loading: false });
}
}
handleConnectionChange = async connectionInfo => {
if (connectionInfo.type === "none" || connectionInfo.type === "unknown") {
Alert.alert(
"You're Offline",
"Please re-connect to update your daily entry.",
[{ text: "OK", onPress: () => console.log("OK Pressed") }],
{ cancelable: false }
);
}
};
_handleAppStateChange = async nextAppState => {
//need to be checking if the token is expired, or close to it, not just always refreshing
if (nextAppState === "active") {
const refreshToken = await AsyncStorage.getItem("refreshToken");
if (refreshToken) {
this.updateAccessToken(refreshToken);
}
}
};
updateAccessToken = refreshToken => {
console.log("Starting refresh token flow");
//TODO: not rejecting this promise...might need to refactor, right now just catching err from auth0 call to get new token and calling logout flow to have user re-auth
return new Promise((resolve, reject) => {
auth0.auth
.refreshToken({ refreshToken })
.then(res => {
console.log("REFRESH TOKEN RES: ", res.expiresIn);
this.setState({ accessToken: res.accessToken }, async () => {
let newTokenExpiration = moment()
.add(res.expiresIn, "seconds")
.valueOf();
let newTokenStr = newTokenExpiration.toString();
await AsyncStorage.multiSet([
["accessToken", res.accessToken],
["tokenExpiration", newTokenStr]
]);
console.log("REFRESHED THE USER W/ NEW ACCESS TOKEN");
resolve(res.accessToken);
});
})
.catch(err => {
console.log("ERRRRR>>>>>", err);
Alert.alert(
"Error getting profile",
"Please login again.",
[{ text: "OK", onPress: () => this.userLogout() }],
{ cancelable: false }
);
});
});
};
shouldComponentUpdate(nextProps, nextState) {
// Don't refresh the UI when getting new accessToken via the refreshToken
// TODO: This looks pretty gross
if (
this.state.accessToken &&
this.state.accessToken !== nextState.accessToken &&
nextState.accessToken !== null
) {
console.log("NOT UPDATING COMPONENT");
return false;
} else {
console.log("UPDATING COMPONENT");
return true;
}
}
showLogin = () => {
// TODO: this looks gross, need to refactor at some point to better handle errors
this.setState({ loggingIn: true }, () => {
auth0.webAuth
.authorize({
scope: "openid offline_access profile",
audience: "https://cleaneatingapi.karlmorand.com"
})
.then(async authUser => {
console.log("AUTH USER: ", authUser);
let tokenExpiration = moment()
.add(authUser.expiresIn, "seconds")
.valueOf();
let tokenStr = tokenExpiration.toString();
await AsyncStorage.setItem("tokenExpiration", tokenStr);
this.setState({ loading: true, loggingIn: false }, () => {
auth0.auth
.userInfo({ token: authUser.accessToken })
.then(async res => {
console.log("AUTH RES: ", res);
try {
await AsyncStorage.setItem(
"refreshToken",
authUser.refreshToken
);
} catch (error) {
console.log("ERROR SAAVING");
this.userLogout();
}
this.getMongoProfile(res.sub, authUser.accessToken);
})
.catch(err => this.userLogout());
});
})
// TODO: Handle this error
.catch(error => this.userLogout());
});
};
getMongoProfile = (authId, accessToken) => {
// TODO: if this fails with a 401 response need to redirect to login/show a message that they need to log in again
console.log("Getting mongo profile");
// TODO: Need better error handling on the AsyncStorage stuff, or just use Realm
const headers = { Authorization: `Bearer ${accessToken}` };
axios
.get(`${this.apiURL}/user/${authId}`, { headers })
.then(async res => {
console.log("RES>>>>", res); //TODO: this if check seems pretty gross
if (res.status >= 400 || !res.data || !res.data.authId) {
Alert.alert(
"Error getting profile",
"Please login again.",
[{ text: "OK", onPress: () => this.userLogout() }],
{ cancelable: false }
);
} else {
await AsyncStorage.multiSet([
["accessToken", accessToken],
["authId", authId],
["mongoId", res.data._id]
]);
this.setState({
mongoUser: res.data,
mongoId: res.data._id,
authId: res.data.authId,
onboardingComplete: res.data.onboardingComplete,
loading: false,
accessToken: accessToken
});
}
})
.catch(err => {
console.log("Couldn't get user profile: ", err);
Alert.alert(
"Error getting profile",
"Please login again.",
[{ text: "OK", onPress: () => this.userLogout() }],
{ cancelable: false }
);
});
};
selectTeam = teamId => {
const headers = { Authorization: `Bearer ${this.state.accessToken}` };
const data = { team: teamId };
if (!this.state.mongoId) {
Alert.alert(
"Error",
"Please login again.",
[{ text: "OK", onPress: () => this.userLogout() }],
{ cancelable: false }
);
}
axios
.post(`${this.apiURL}/team/${this.state.mongoId}`, data, { headers })
.then(res => {
console.log("TEAM RES: ", res.data);
//TODO: need a better solution then gettting the mongo profile again, since the updated profile is already being sent down, but it doesn't have the gym name populated
// this.getMongoProfile(this.state.authId, this.state.accessToken)
this.setState({ showTeamPicker: false, mongoUser: res.data });
})
.catch(err => {
console.log(err);
});
};
userLogout = () => {
AsyncStorage.multiRemove(
[
"accessToken",
"authId",
"onboardingComplete",
"mongoId",
"refreshToken"
],
() => {
if (Platform.OS === "android") {
this.setState({
accessToken: null,
authId: null,
mongoId: null,
mongoUser: null,
loading: false,
showTeamPicker: false,
loggingIn: false
});
} else {
auth0.webAuth
.clearSession({})
.then(success => {
this.setState({
accessToken: null,
authId: null,
mongoId: null,
mongoUser: null,
loading: false,
showTeamPicker: false,
loggingIn: false
});
})
.catch(error => console.log(error));
}
}
);
};
showTeamPicker = () => {
this.setState({ showTeamPicker: true });
};
onboardUser = challengeLevel => {
const headers = { Authorization: `Bearer ${this.state.accessToken}` };
const data = { challengeLevel: challengeLevel };
if (!this.state.mongoId) {
Alert.alert(
"Error",
"Please login again.",
[{ text: "OK", onPress: () => this.userLogout() }],
{ cancelable: false }
);
}
axios
.post(
`${this.apiURL}/user/${this.state.mongoId}/setup`,
{ data },
{ headers }
)
.then(async res => {
await AsyncStorage.setItem("onboardingComplete", "true");
this.setState({
mongoUser: res.data,
onboardingComplete: res.data.onboardingComplete,
showTeamPicker: true,
loading: false
});
})
.catch(err => console.log(err));
};
render() {
console.ignoredYellowBox = ["Remote debugger"];
if (!this.state.accessToken && !this.state.loading) {
return (
<SafeAreaView style={styles.safeArea}>
<View style={styles.container}>
{this.state.loggingIn ? (
<ActivityIndicator size="large" color="#0000ff" />
) : (
<Login handlePress={this.showLogin} />
)}
</View>
</SafeAreaView>
);
}
if (this.state.loading) {
return (
<SafeAreaView style={styles.safeArea}>
<View style={styles.container}>
<ActivityIndicator size="large" color="#0000ff" />
</View>
</SafeAreaView>
);
}
if (this.state.showTeamPicker) {
return (
<SafeAreaView style={styles.safeArea}>
<TeamPicker
closeModal={this.selectTeam}
visible={this.state.showTeamPicker}
gymId={this.state.mongoUser.gym._id}
accessToken={this.state.accessToken}
/>
</SafeAreaView>
);
}
if (!this.state.onboardingComplete) {
return (
<SafeAreaView style={styles.safeArea}>
<Onboarding onboardUser={this.onboardUser} />
</SafeAreaView>
);
}
if (!this.state.mongoId) {
console.log("NO MONGO ID");
Alert.alert(
"Error getting profile",
"Please login again.",
[{ text: "OK", onPress: () => this.userLogout() }],
{ cancelable: false }
);
return (
<SafeAreaView style={styles.safeArea}>
<View style={styles.container}>
<ActivityIndicator size="large" color="#0000ff" />
</View>
</SafeAreaView>
);
}
return (
<Router
user={this.state.mongoUser}
authId={this.state.authId}
mongoId={this.state.mongoId}
accessToken={this.state.accessToken}
logout={this.userLogout}
showTeamPicker={this.showTeamPicker}
/>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
backgroundColor: "#F5FCFF",
paddingLeft: 5,
paddingRight: 5
},
welcome: {
fontSize: 20,
textAlign: "center",
margin: 10
},
instructions: {
textAlign: "center",
color: "#333333",
marginBottom: 5
},
safeArea: {
flex: 1
}
});