forked from ss1121/FKP-REST
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
95 lines (80 loc) · 2.99 KB
/
github.js
File metadata and controls
95 lines (80 loc) · 2.99 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
/**
* Module dependencies.
*/
var fs = require('fs')
var path = require('path')
var libs = require('../libs/libs')
var config = require('../config');
var api = require('../apis/javaapi');
function *github(){
libs.clog('github第三方登陆: '+__filename)
var github = config.auth.github;
var jump_url = config.auth.github.successUrl
var query = this.local.query
if (process.env.env === 'test') {
github = config.test.auth.github
jump_url = github.successUrl
}
var _this = this;
var cat = this.params.cat
var title = this.params.title;
var client_id = github.clientID,
scope = 'user',
stat = 'agzgz',
cb_url = github.callbackURL,
secret = github.clientSecret,
sesskey = github.userKey;
if (title ==='sign' && !this.session.$user){
this.redirect("https://github.com/login?return_to=/login/oauth/authorize?client_id="+client_id+"&redirect_uri="+cb_url+"&response_type=code")
}
else {
if (this.session.$user){
this.redirect(jump_url)
}
if (query.code){
var code = query.code;
var postdata = {
client_id: client_id,
client_secret: secret,
code: code,
redirect_uri: cb_url,
method : 'post'
}
var ttt = yield api.req(this, 'https://github.com/login/oauth/access_token', postdata)
var github_token = ttt[1].access_token
// github_token为如下内容
// { access_token: '82b79fcd53b5532fd6fe91d8281edf80677ae4de',
// token_type: 'bearer',
// scope: '' }
var userpost = {
method: 'get',
headers: github.headers,
"access_token": github_token
}
var github_user = yield api.req(this, 'https://api.github.com/user', userpost)
// var github_user = yield api.req(this, 'https://api.github.com/user?access_token='+github_token, userpost)
var g_user = JSON.parse(github_user[1])
libs.elog('github user info')
console.log(typeof g_user);
console.log(g_user);
var hasUser = yield api.req(this, '$signis', {username: g_user.login})
if (hasUser){
// this.session[sesskey] = hasUser
libs.elog('session user info')
console.log(this.session.$user);
this.redirect(jump_url)
}
else{
libs.elog('write user info into db')
var signupUser = yield api.req(this, '$signup', {github: g_user})
console.log('========== signupUser');
console.log('========== signupUser');
console.log('========== signupUser');
console.log(signupUser);
this.session.$user = signupUser;
this.redirect(jump_url)
}
}
}
}
module.exports = github