-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
382 lines (344 loc) · 10.7 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
import React from "react";
import {
StyleSheet,
Text,
View,
Button,
ActivityIndicator,
AsyncStorage,
StatusBar,
TouchableOpacity,
Image,
Switch
} from "react-native";
import { Constants, WebBrowser, AuthSession } from "expo";
import {
createSwitchNavigator,
createStackNavigator,
createBottomTabNavigator
} from "react-navigation";
import Ionicons from "react-native-vector-icons/Ionicons";
import SafeMap from "./components/SafeMap.js";
// Do Not commit API secrets!!!
// Need to figure out a good strategy for handling keys
// to prevent accidental leaking via version control
const STRAVA_CLIENT_ID = null;
const STRAVA_CLIENT_SECRET = null;
const styles = StyleSheet.create({
button: {
alignItems: "center",
backgroundColor: "#DDDDDD",
padding: 12,
borderRadius: 3,
marginTop: 5
}
});
class HomeScreen extends React.Component {
static navigationOptions = { title: "Home" };
state = {
tokenInfo: null
};
constructor(props) {
console.log("at homescreen");
super(props);
this.state = {
profile: null,
tokenInfo: {
access_token: null,
refresh_token: null,
expires_at: null,
athlete_id: null
}
};
this._loadTokenAsync();
}
_loadTokenAsync = async () => {
let access_token = await AsyncStorage.getItem("access_token", err => {
console.log("error with getting access token: " + err);
});
let refresh_token = await AsyncStorage.getItem("refresh_token", err => {
console.log("error with getting refresh token: " + err);
});
let expires_at = await AsyncStorage.getItem("expires_at", err => {
console.log("error with getting expiration: " + err);
});
let athlete_id = await AsyncStorage.getItem("athlete_id", err => {
console.log("error with getting athlete_id: " + err);
});
console.log("begin load token async homescreen");
this.setState({
tokenInfo: {
access_token: access_token,
refresh_token: refresh_token,
expires_at: expires_at,
athlete_id: athlete_id
}
});
console.log("strava token info from homescreen: ");
console.log(this.state.tokenInfo);
};
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<SafeMap tokenInfo={this.state.tokenInfo} />
</View>
);
}
}
class SettingsScreen extends React.Component {
static navigationOptions = { title: "Settings" };
state = {
profile: null
};
constructor(props) {
super(props);
this._getProfileAsync();
}
_getProfileAsync = async () => {
let profileURL = await AsyncStorage.getItem("profile", err => {
console.log("error with getting profile: " + err);
});
let firstname = await AsyncStorage.getItem("firstname", err => {
console.log("error with getting firstname: " + err);
});
console.log(" get profile async: " + profileURL);
console.log(" get first name async: " + firstname);
this.setState({ profile: profileURL, firstname: firstname });
};
_signOutAsync = async () => {
let access_token = await AsyncStorage.getItem("access_token", err => {
console.log("error with getting access token: " + err);
});
await AsyncStorage.clear();
try {
let response = await fetch("https://www.strava.com/oauth/deauthorize", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
access_token: access_token
})
});
console.log("request successful");
let json = await response.json();
console.log("extracted json from request");
console.log(json);
} catch (err) {
console.log("error: " + err);
}
this.props.navigation.navigate("Auth");
console.log("signing out and clearing async storage");
};
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text>Settings Screen</Text>
<Image
style={{
width: 125,
height: 125,
borderRadius: 25,
shadowOffset: { width: 10, height: 10 },
shadowColor: "grey",
shadowOpacity: 0.8,
margin: 5
}}
source={{
uri: this.state.profile
? this.state.profile
: "https://i.kym-cdn.com/photos/images/original/000/581/296/c09.jpg"
}}
/>
<Text> {this.state.firstname ? this.state.firstname : ""} </Text>
<Button title="Sign Out" onPress={this._signOutAsync} />
</View>
);
}
}
class AuthLoadingScreen extends React.Component {
constructor(props) {
super(props);
this._bootstrapAsync();
}
_bootstrapAsync = async () => {
try {
const access_token = await AsyncStorage.getItem("access_token");
const profile = await AsyncStorage.getItem("profile");
if (access_token !== null && profile !== null) {
console.log("found access token: " + access_token);
console.log("navigating to home screen");
this.props.navigation.navigate("App");
} else {
console.log("no access token stored, navigating to signin screen");
this.props.navigation.navigate("Auth");
}
} catch (err) {
console.log("no access token stored, navigating to signin screen");
this.props.navigation.navigate("Auth");
}
};
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<ActivityIndicator />
<StatusBar barStyle="default" />
</View>
);
}
}
class SignInScreen extends React.Component {
static navigationOptions = {
title: "Please sign in"
};
state = {
code: null
};
render() {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<TouchableOpacity onPress={this._stravaSignInAsync}>
<Image
source={require("./assets/btn_strava_connectwith_orange.png")}
/>
</TouchableOpacity>
<TouchableOpacity style={styles.button} onPress={this._signInAsync}>
<Text> Just take me to the app </Text>
</TouchableOpacity>
</View>
);
}
_signInAsync = async () => {
this.props.navigation.navigate("App");
};
_stravaSignInAsync = async () => {
let redirectUrl = AuthSession.getRedirectUrl();
console.log(redirectUrl);
let result = await AuthSession.startAsync({
authUrl:
`https://www.strava.com/oauth/authorize?client_id=${STRAVA_CLIENT_ID}` +
`&response_type=code&redirect_uri=${redirectUrl}&approval_prompt=force`
}).catch(err => {
console.log(err);
});
console.log(result);
if (result.type === "success" && result.params !== null) {
let authToken = result.params.code;
console.log("auth token: " + authToken);
try {
let response = await fetch("https://www.strava.com/oauth/token", {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
client_id: STRAVA_CLIENT_ID,
client_secret: STRAVA_CLIENT_SECRET,
code: authToken,
grant_type: "authorization_code"
})
});
console.log("request successful");
let json = await response.json();
console.log("extracted json from request");
console.log(json);
if (json.refresh_token !== null && json.refresh_token !== undefined) {
console.log("before storing refresh_token: " + json.refresh_token);
await AsyncStorage.setItem(
"refresh_token",
json.refresh_token,
err => {
console.log("error with refresh token store: " + err);
}
);
console.log("stored refresh_token");
}
if (json.expires_at !== null && json.expires_at !== undefined) {
console.log("before storing expiration: " + json.expires_at);
await AsyncStorage.setItem("expires_at", json.expires_at, err => {
console.log("error with expiration store: " + err);
});
console.log("stored expiration");
}
await AsyncStorage.setItem("profile", json.athlete.profile, err => {
console.log("error with profile store: " + err);
});
await AsyncStorage.setItem("firstname", json.athlete.firstname, err => {
console.log("error with firstname store: " + err);
});
if (
json.athlete !== undefined &&
json.athlete !== null &&
json.athlete.id !== undefined &&
json.athlete.id !== null
) {
let athlete_id = json.athlete.id;
console.log("before storing athlete id: " + athlete_id);
await AsyncStorage.setItem("athlete_id", "" + athlete_id, err => {
console.log("error with athlete id store: " + err);
});
console.log("stored athlete id");
}
if (json.access_token !== null && json.access_token !== undefined) {
console.log("before storing access token: " + json.access_token);
await AsyncStorage.setItem("access_token", json.access_token, err => {
console.log("error with access token store: " + err);
});
console.log("stored token info");
this.props.navigation.navigate("App");
} else {
console.log("something went wrong");
}
} catch (err) {
console.log("error: " + err);
}
}
};
}
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
const AppTab = createBottomTabNavigator(
{ Home: HomeScreen, Settings: SettingsScreen },
{
navigationOptions: ({ navigation }) => ({
tabBarIcon: ({ focused, horizontal, tintColor }) => {
const { routeName } = navigation.state;
let iconName;
if (routeName === "Home") {
iconName = `ios-map${focused ? "" : "-outline"}`;
} else if (routeName === "Settings") {
iconName = `ios-options${focused ? "" : "-outline"}`;
}
return (
<Ionicons
name={iconName}
size={horizontal ? 20 : 25}
color={tintColor}
/>
);
}
}),
tabBarOptions: {
activeTintColor: "tomato",
inactiveTintColor: "gray"
}
}
);
const RootStack = createSwitchNavigator(
{
AuthLoading: AuthLoadingScreen,
App: AppTab,
Auth: AuthStack
},
{
initialRouteName: "AuthLoading"
}
);
export default class App extends React.Component {
render() {
return <RootStack />;
}
}
// oauth url strava rnnr app:
//