Skip to content

Commit e9bf1da

Browse files
committed
feat/ save access token in state on redirect success
1 parent 722904b commit e9bf1da

File tree

5 files changed

+60
-30
lines changed

5 files changed

+60
-30
lines changed

iOS/main.jsbundle

+1-1
Large diffs are not rendered by default.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"dependencies": {
99
"react-native": "^0.5.0",
10-
"react-native-navbar": "^0.5.1"
10+
"react-native-navbar": "^0.5.1",
11+
"shitty-qs": "^1.0.1"
1112
}
1213
}

src/app.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class App extends React.Component {
2424
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
2525
this.state = {
2626

27+
redirect: null,
28+
2729
apis: ds.cloneWithRows([
2830
'Github',
2931
'Dropbox',
@@ -34,20 +36,15 @@ class App extends React.Component {
3436
'Wordpress'
3537
]),
3638

37-
acess_token: {
38-
github: null,
39-
dropbox: null,
40-
google: null,
41-
facebook: null,
42-
twitter: null,
43-
yahoo: null,
44-
wordpress: null
45-
}
39+
github_token: null
4640

4741
};
4842
}
4943

5044
render(){
45+
if (this.state.redirect){
46+
return (<Text>{this.state.redirect}</Text>)
47+
}
5148
return (
5249
<ListView
5350
dataSource={this.state.apis}
@@ -68,12 +65,13 @@ class App extends React.Component {
6865

6966
authenticate(api){
7067
// invoke the proper api oauth function on click
71-
oauth[api.toLowerCase()]();
72-
68+
var invokeOAuth = oauth[api.toLowerCase()]
69+
invokeOAuth(this.handleAuthSuccess.bind(this));
7370
}
7471

75-
handleAuthSuccess(query){
76-
console.log('*******SUCESS*******', query);
72+
handleAuthSuccess(token){
73+
console.log('*************', token)
74+
this.setState({ github_token: token, redirect: 'github' });
7775
}
7876

7977
}

src/utils/github-api.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
var config = require('../../config.js');
4+
5+
module.exports = {
6+
7+
getAccessToken: function(code){
8+
return fetch('https://github.com/login/oauth/access_token', {
9+
method: 'post',
10+
headers: {
11+
'Accept': 'application/json',
12+
'Content-Type': 'application/json'
13+
},
14+
body: JSON.stringify({
15+
code: code,
16+
client_id: config.github.client_id,
17+
client_secret: config.github.client_secret
18+
})
19+
}).then((response) => {
20+
var access_token = JSON.parse(response._bodyInit).access_token;
21+
return access_token;
22+
})
23+
}
24+
25+
};

src/utils/oauth.js

+21-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
1-
21
var { LinkingIOS, AlertIOS } = require('react-native');
32

4-
var config = require('../../config.js');
5-
var githubClientID = config.github;
3+
var qs = require('shitty-qs');
4+
var GitHubAPI = require('./github-api');
5+
6+
var config = require('../../config');
7+
var githubClientID = config.github.client_id;
68

79
module.exports = {
810

9-
github: function(){
11+
github: function(callback){
1012

11-
var handleUrl = function(event) {
12-
console.log(event.url)
13-
LinkingIOS.removeEventListener('url', handleUrl);
14-
}
13+
var handleUrl = function(event) {
14+
var code = qs(event.url.match(/\?(.*)/)[1]).code;
15+
GitHubAPI.getAccessToken(code)
16+
.then((access_token) => {
17+
callback(access_token);
18+
})
19+
LinkingIOS.removeEventListener('url', handleUrl);
20+
};
1521

16-
LinkingIOS.addEventListener('url', handleUrl);
22+
LinkingIOS.addEventListener('url', handleUrl);
1723

1824
LinkingIOS.openURL([
1925
'https://github.com/login/oauth/authorize',
@@ -24,27 +30,27 @@ module.exports = {
2430
},
2531

2632
google: function(){
27-
AlertIOS.alert('Hey!', 'Still workin on it');
33+
AlertIOS.alert('Hey!', 'Still workin on it');
2834
},
2935

3036
twitter: function(){
31-
AlertIOS.alert('Hey!', 'Still workin on it');
37+
AlertIOS.alert('Hey!', 'Still workin on it');
3238
},
3339

3440
dropbox: function(){
35-
AlertIOS.alert('Hey!', 'Still workin on it');
41+
AlertIOS.alert('Hey!', 'Still workin on it');
3642
},
3743

3844
facebook: function(){
39-
AlertIOS.alert('Hey!', 'Still workin on it');
45+
AlertIOS.alert('Hey!', 'Still workin on it');
4046
},
4147

4248
yahoo: function(){
43-
AlertIOS.alert('Hey!', 'Still workin on it');
49+
AlertIOS.alert('Hey!', 'Still workin on it');
4450
},
4551

4652
wordpress: function(){
47-
AlertIOS.alert('Hey!', 'Still workin on it');
53+
AlertIOS.alert('Hey!', 'Still workin on it');
4854
}
4955

5056
};

0 commit comments

Comments
 (0)