-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
235 lines (227 loc) · 6.67 KB
/
App.tsx
File metadata and controls
235 lines (227 loc) · 6.67 KB
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
import React from 'react';
import {
View,
Modal,
Button,
Alert,
Dimensions,
TouchableOpacity,
Text,
} from 'react-native';
import CookieManager from 'react-native-cookies';
import WebView from 'react-native-webview';
import AsyncStorage from '@react-native-community/async-storage';
let Dim = {
width: Dimensions.get('window').width,
height: Dimensions.get('window').height,
};
class credentials {
clientId: string;
clientSecret: string;
scope: string;
response_type: string;
state: string;
redirect_uri: string;
constructor(
_clientId: string,
_clientSecret: string,
_scope: string,
_response_type: string,
_state: string,
_redirect_uri: string,
) {
this.clientId = _clientId;
this.clientSecret = _clientSecret;
this.scope = _scope;
this.response_type = _response_type;
this.state = _state;
this.redirect_uri = _redirect_uri;
}
}
const config: credentials = {
clientId: '<Redacted>',
clientSecret: '<Redacted>',
scope: 'r_emailaddress,r_liteprofile,w_member_social',
response_type: 'code',
state: '<Redacted>',
redirect_uri: '<Redacted>',
};
function getURLForAuth(_config: credentials): string {
let _baseURL = 'https://www.linkedin.com/oauth/v2/authorization?';
_baseURL += 'client_id=' + _config.clientId + '&'; // adding clientID
_baseURL += 'response_type=' + _config.response_type + '&'; //adding response type
_baseURL += 'redirect_uri=' + _config.redirect_uri + '&'; //add redirect uri
_baseURL += 'state=' + _config.state + '&'; // add state
_baseURL += 'scope=' + _config.scope; // add scopes
return _baseURL;
}
class App extends React.Component<any, any> {
state = {
showLoginWebview: false,
linkedIn_Code: null,
linkedin_accessToken: null,
linkedin_expiresIn: null,
};
_handleNav = (obj: any) => {
var url = decodeURIComponent(obj.url);
const hashes = url.slice(url.indexOf('?') + 1).split('&');
const params: any = {};
hashes.map((hash) => {
const [key, val] = hash.split('=');
params[key] = decodeURIComponent(val);
});
hashes.forEach((value) => {
const [key, val] = value.split('=');
console.log(key, decodeURIComponent(val));
params[key.replace('"', '')] = decodeURIComponent(val);
});
if (params.code) {
console.log({token: params.code, status: 'sucess'});
this.setState({showLoginWebview: false, linkedIn_Code: params.code});
CookieManager.clearAll();
this.fetchAcessToken(params.code, config);
} else if (params.error) {
alert(
'Error occured : ' +
params.error +
'\nError Description: ' +
params.error_description,
);
CookieManager.clearAll();
}
};
render(): any {
return (
<View style={{flex: 1}}>
{this.state.showLoginWebview ? (
<Modal
animationType="fade"
transparent={true}
visible={this.state.showLoginWebview}
onRequestClose={() => {
CookieManager.clean();
this.setState({
showLoginWebview: false,
});
}}
hardwareAccelerated={true}>
<View
style={{
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: 'rgba(0,0,0,0.75)',
back: 0.5,
}}>
<View
style={{
flex: 1,
borderRadius: 40,
}}>
<WebView
source={{uri: getURLForAuth(config)}}
style={{
maxHeight: Dim.height * 0.9,
width: Dim.width * 0.9,
maxWidth: Dim.width * 0.9,
}}
onNavigationStateChange={this._handleNav}
/>
</View>
<TouchableOpacity
style={{
backgroundColor: 'orange',
height: Dim.height * 0.05,
width: Dim.width * 0.75,
margin: 10,
alignItems: 'center',
justifyContent: 'center',
}}
onPress={() => {
CookieManager.clean();
this.setState({
showLoginWebview: false,
});
}}>
<Text
style={{
fontSize: Dim.height * 0.04 - 20,
color: 'white',
}}>
CLOSE
</Text>
</TouchableOpacity>
</View>
</Modal>
) : null}
{this.state.linkedin_accessToken ? (
<Button
title="logout"
onPress={async () => {
this.setState({linkedin_accessToken: null});
await AsyncStorage.setItem('linkedinAccessToken', '');
}}
/>
) : (
<Button
title="login"
onPress={() =>
this.setState({
showLoginWebview: true,
})
}
/>
)}
</View>
);
}
fetchAcessToken = async (codeVal: string, _config: credentials) => {
let _string =
'grant_type=authorization_code&' +
'code=' +
codeVal +
'&redirect_uri=' +
_config.redirect_uri;
_string +=
'&client_id=' +
_config.clientId +
'&client_secret=' +
_config.clientSecret;
const returnValue: any = await fetch(
'https://www.linkedin.com/oauth/v2/accessToken',
{
method: 'POST',
body: _string,
headers: {
'Content-type': 'application/x-www-form-urlencoded',
},
},
).catch((error: any) => {
console.log(error);
});
const result: any = await returnValue.json();
console.log(result);
this.setState({
linkedin_accessToken: result.access_token,
linkedin_expiresIn: result.expires_in,
});
await AsyncStorage.setItem('linkedinAccessToken', result.access_token, () =>
alert('Login Successful'),
).catch((error: any) => console.log(error));
await AsyncStorage.setItem(
'linkedinExpiresIn',
result.expires_in.toString(),
).catch((error) => console.log(error));
};
async componentDidMount(): Promise<any> {
this.setState({
linkedin_accessToken: await AsyncStorage.getItem('linkedinAccessToken'),
linkedin_expiresIn: await AsyncStorage.getItem('linkedinExpiresIn'),
});
console.log(
await AsyncStorage.getItem('linkedinAccessToken'),
await AsyncStorage.getItem('linkedinExpiresIn'),
);
}
}
export default App;