forked from cnodejs/nodeclub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
132 lines (123 loc) · 4.39 KB
/
github.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
var Models = require('../models');
var User = Models.User;
var authMiddleWare = require('../middlewares/auth');
var tools = require('../common/tools');
var eventproxy = require('eventproxy');
var uuid = require('node-uuid');
var validator = require('validator');
exports.callback = function (req, res, next) {
var profile = req.user;
var email = profile.emails && profile.emails[0] && profile.emails[0].value;
User.findOne({githubId: profile.id}, function (err, user) {
if (err) {
return next(err);
}
// 当用户已经是 cnode 用户时,通过 github 登陆将会更新他的资料
if (user) {
user.githubUsername = profile.username;
user.githubId = profile.id;
user.githubAccessToken = profile.accessToken;
// user.loginname = profile.username;
user.avatar = profile._json.avatar_url;
user.email = email || user.email;
user.save(function (err) {
if (err) {
// 根据 err.err 的错误信息决定如何回应用户,这个地方写得很难看
if (err.message.indexOf('duplicate key error') !== -1) {
if (err.message.indexOf('email') !== -1) {
return res.status(500)
.render('sign/no_github_email');
}
if (err.message.indexOf('loginname') !== -1) {
return res.status(500)
.send('您 GitHub 账号的用户名与之前在 CNodejs 注册的用户名重复了');
}
}
return next(err);
}
authMiddleWare.gen_session(user, res);
return res.redirect('/');
});
} else {
// 如果用户还未存在,则建立新用户
req.session.profile = profile;
return res.redirect('/auth/github/new');
}
});
};
exports.new = function (req, res, next) {
res.render('sign/new_oauth', {actionPath: '/auth/github/create'});
};
exports.create = function (req, res, next) {
var profile = req.session.profile;
var isnew = req.body.isnew;
var loginname = validator.trim(req.body.name || '').toLowerCase();
var password = validator.trim(req.body.pass || '');
var ep = new eventproxy();
ep.fail(next);
if (!profile) {
return res.redirect('/signin');
}
delete req.session.profile;
var email = profile.emails && profile.emails[0] && profile.emails[0].value;
if (isnew) { // 注册新账号
var user = new User({
loginname: profile.username,
pass: profile.accessToken,
email: email,
avatar: profile._json.avatar_url,
githubId: profile.id,
githubUsername: profile.username,
githubAccessToken: profile.accessToken,
active: true,
accessToken: uuid.v4(),
});
user.save(function (err) {
if (err) {
// 根据 err.err 的错误信息决定如何回应用户,这个地方写得很难看
if (err.message.indexOf('duplicate key error') !== -1) {
if (err.message.indexOf('email') !== -1) {
return res.status(500)
.render('sign/no_github_email');
}
if (err.message.indexOf('loginname') !== -1) {
return res.status(500)
.send('您 GitHub 账号的用户名与之前在 CNodejs 注册的用户名重复了');
}
}
return next(err);
// END 根据 err.err 的错误信息决定如何回应用户,这个地方写得很难看
}
authMiddleWare.gen_session(user, res);
res.redirect('/');
});
} else { // 关联老账号
ep.on('login_error', function (login_error) {
res.status(403);
res.render('sign/signin', { error: '账号名或密码错误。' });
});
User.findOne({loginname: loginname},
ep.done(function (user) {
if (!user) {
return ep.emit('login_error');
}
tools.bcompare(password, user.pass, ep.done(function (bool) {
if (!bool) {
return ep.emit('login_error');
}
user.githubUsername = profile.username;
user.githubId = profile.id;
// user.loginname = profile.username;
user.avatar = profile._json.avatar_url;
user.githubAccessToken = profile.accessToken;
user.save(function (err) {
if (err) {
return next(err);
}
authMiddleWare.gen_session(user, res);
res.redirect('/');
});
}));
}));
}
};