Skip to content

Commit 9caa574

Browse files
authored
fix: add githubId into user model
* fix: login with github * fix: add githubId into user model
1 parent 8e58d71 commit 9caa574

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

app.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module.exports = app => {
4444
// 用户不存在则创建
4545
if (!existUser) {
4646
existUser = new ctx.model.User();
47+
existUser.githubId = profile.id;
4748
existUser.active = true;
4849
}
4950

@@ -53,7 +54,26 @@ module.exports = app => {
5354
existUser.avatar = profile._json.avatar_url;
5455
existUser.githubUsername = profile.username;
5556
existUser.githubAccessToken = profile.accessToken;
56-
await existUser.save();
57+
58+
try {
59+
await existUser.save();
60+
} catch (ex) {
61+
if (ex.message.indexOf('duplicate key error') !== -1) {
62+
let err;
63+
if (ex.message.indexOf('email') !== -1) {
64+
err = new Error('您 GitHub 账号的 Email 与之前在 CNodejs 注册的用户名重复了');
65+
err.code = 'duplicate_email';
66+
throw err;
67+
}
68+
69+
if (ex.message.indexOf('loginname') !== -1) {
70+
err = new Error('您 GitHub 账号的用户名与之前在 CNodejs 注册的用户名重复了');
71+
err.code = 'duplicate_loginname';
72+
throw err;
73+
}
74+
}
75+
throw ex;
76+
}
5777

5878
return existUser;
5979
};

app/middleware/create_user_limit.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const moment = require('moment');
44

5-
module.exports = limitCount => {
5+
module.exports = limit => {
66

77
return async function createUserLimit(ctx, next) {
88
const { service } = ctx;
@@ -11,12 +11,12 @@ module.exports = limitCount => {
1111
const YYYYMMDD = moment().format('YYYYMMDD');
1212
const key = `user_count_${realIP}_${YYYYMMDD}`;
1313

14-
let todayTopicsCount = (await service.cache.get(key)) || 0;
15-
if (todayTopicsCount >= limitCount) {
14+
let count = (await service.cache.get(key)) || 0;
15+
if (count >= limit) {
1616
ctx.status = 403;
1717
ctx.body = {
1818
success: false,
19-
error_msg: '频率限制:当前操作每天可以进行 ' + limitCount + ' 次',
19+
error_msg: '频率限制:当前操作每天可以进行 ' + limit + ' 次',
2020
};
2121
return;
2222
}
@@ -25,10 +25,10 @@ module.exports = limitCount => {
2525

2626
if (ctx.status === 302) {
2727
// 新建话题成功
28-
todayTopicsCount += 1;
28+
count += 1;
2929
await service.cache.incr(key, 60 * 60 * 24);
30-
ctx.set('X-RateLimit-Limit', limitCount);
31-
ctx.set('X-RateLimit-Remaining', limitCount - todayTopicsCount);
30+
ctx.set('X-RateLimit-Limit', limit);
31+
ctx.set('X-RateLimit-Remaining', limit - count);
3232
}
3333
};
3434
};

app/router.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ module.exports = app => {
6464
router.post('/user/:name/block', adminRequired, user.block); // 禁言某用户
6565
router.post('/user/:name/delete_all', adminRequired, user.deleteAll); // 删除某用户所有发言
6666

67-
// // message controler
67+
// message controler
6868
router.get('/my/messages', userRequired, message.index); // 用户个人的所有消息页
6969

7070
// topic
7171

72-
// // 新建文章界面
72+
// 新建文章界面
7373
router.get('/topic/create', userRequired, topic.create);
7474

7575
router.get('/topic/:tid', topic.index); // 显示某个话题
@@ -80,14 +80,14 @@ module.exports = app => {
8080

8181
router.post('/topic/:tid/delete', userRequired, topic.delete);
8282

83-
// // 保存新建的文章
83+
// 保存新建的文章
8484
router.post('/topic/create', userRequired, createTopicLimit, topic.put);
8585

8686
router.post('/topic/:tid/edit', userRequired, topic.update);
8787
router.post('/topic/collect', userRequired, topic.collect); // 关注某话题
8888
router.post('/topic/de_collect', userRequired, topic.de_collect); // 取消关注某话题
8989

90-
// // reply controller
90+
// reply controller
9191
router.post('/:topic_id/reply', userRequired,
9292
// limit.peruserperday('create_reply', config.create_reply_per_day, { showJson: false }),
9393
reply.add); // 提交一级回复

0 commit comments

Comments
 (0)