-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
145 lines (132 loc) · 3.99 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
import React from 'react';
import { StyleSheet, View, Linking, Platform, ActivityIndicator } from 'react-native';
import { Constants } from 'expo';
import QueryString from 'query-string';
import moment from 'moment';
import Config from './app/config/default.json';
import { OReillyApp } from './app/config/router';
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center'
}
});
export default class App extends React.Component {
state = {
isLoggedIn: true,
};
componentDidMount() {
if (!this.state.isLoggedIn) {
this.authenticate();
}
}
componentWillUnmount() {
Linking.removeEventListener('url', this.handleOpenURL);
}
getAuthorization = (code) => {
console.log('OAuth getToken Url: ', Config.oauth.tokenUrl);
const body = {
redirect_uri: Constants.linkingUri,
client_id: Config.oauth.clientId,
grant_type: 'authorization_code',
code,
scope: Config.oauth.scope
};
return fetch(Config.oauth.tokenUrl, {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
});
}
checkTokenExpired = () => {
if (moment().unix() > this.auth.expiresAt) {
return true;
}
return false;
}
doLoginWithToken = () => {
if (this.checkTokenExpired()) {
console.log('The access_token has expired. try to get new token with refresh_token.');
} else {
this.setState({ isLoggedIn: true });
}
}
handleOpenURL = (event) => {
console.log(`handleOpenURL(${JSON.stringify(event)})`);
const query = event.url.match(/\?(.*)/);
if (query) {
const { code, state } = QueryString.parse(query[1]);
console.log(`code:${code}, state:${state}`);
if (code && state && state === Config.oauth.state) {
if (state === Config.oauth.state) {
const body = {
redirect_uri: Constants.linkingUri,
client_id: Config.oauth.clientId,
grant_type: 'authorization_code',
code,
scope: Config.oauth.scope
};
fetch(Config.oauth.tokenUrl, {
method: 'POST',
body: QueryString.stringify(body),
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log('authorized:', response);
this.auth = { ...response };
this.auth.expiresAt = moment().unix() + auth.expires_in;
this.doLoginWithToken();
if (Platform.OS === 'ios') {
// Linking.removeEventListener('url', this.handleOpenURL);
}
}).catch(error => {
console.log('getAuthorization() error:', error.request);
});
}
}
}
}
authenticate() {
if (Platform.OS === 'android') {
Linking.getInitialURL().then((url) => {
this.handleOpenURL({ url });
});
} else {
Linking.addEventListener('url', this.handleOpenURL);
}
const authUrl = `${Config.oauth.authorizationUrl}?${QueryString.stringify({
client_id: Config.oauth.clientId,
redirect_uri: Constants.linkingUri,
response_type: Config.oauth.response_type,
scope: Config.oauth.scope,
state: Config.oauth.state
})}`;
Linking.canOpenURL(authUrl).then((supported) => {
if (!supported) {
console.log(`Can't handle url: ${authUrl}`);
} else {
console.log(`Redirecting to ${authUrl}`);
Linking.openURL(authUrl)
.catch(error => console.log(`Can't open url: ${error}`))
.then(() => {
console.log('success in linking');
});
}
}).catch(err => console.error('An error occurred', err));
}
render() {
const { isLoggedIn } = this.state;
return (
<View style={styles.container}>
{ isLoggedIn ?
<OReillyApp />
:
<ActivityIndicator size="large" color="#00ff00" />
}
</View>
);
}
}