Skip to content

Commit f4edbf7

Browse files
thonatosJacksonTian
authored andcommitted
refactor: move handler out from passport.verify
1 parent 0660fc3 commit f4edbf7

File tree

1 file changed

+57
-61
lines changed

1 file changed

+57
-61
lines changed

app.js

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,76 +7,72 @@ module.exports = app => {
77
app.config.coreMiddleware.unshift('less');
88
}
99

10-
app.passport.verify(async (ctx, user) => {
11-
ctx.logger.debug('passport.verify', user);
12-
13-
const localHandler = async ({ username, password }) => {
14-
const getUser = username => {
15-
if (username.indexOf('@') > 0) {
16-
return ctx.service.user.getUserByMail(username);
17-
}
18-
return ctx.service.user.getUserByLoginName(username);
19-
};
20-
const existUser = await getUser(username);
21-
22-
// 用户不存在
23-
if (!existUser) {
24-
return null;
10+
const localHandler = async (ctx, { username, password }) => {
11+
const getUser = username => {
12+
if (username.indexOf('@') > 0) {
13+
return ctx.service.user.getUserByMail(username);
2514
}
15+
return ctx.service.user.getUserByLoginName(username);
16+
};
17+
const existUser = await getUser(username);
2618

27-
const passhash = user.pass;
28-
const equal = tools.compare(passhash, password);
19+
// 用户不存在
20+
if (!existUser) {
21+
return null;
22+
}
2923

30-
// 密码不匹配
31-
if (!equal) {
32-
return null;
33-
}
24+
const passhash = existUser.pass;
25+
const equal = tools.compare(passhash, password);
3426

35-
// 用户未激活
36-
if (!existUser.active) {
37-
// 发送激活邮件
38-
return null;
39-
}
27+
// 密码不匹配
28+
if (!equal) {
29+
return null;
30+
}
4031

41-
// 验证通过
42-
return existUser;
43-
};
32+
// 用户未激活
33+
if (!existUser.active) {
34+
// 发送激活邮件
35+
return null;
36+
}
4437

45-
const githubHandler = async ({ profile }) => {
46-
const email = profile.emails && profile.emails[0] && profile.emails[0].value;
47-
const existUser = await ctx.service.user.getUsersByQuery({
48-
githubId: profile.id,
49-
});
38+
// 验证通过
39+
return existUser;
40+
};
5041

51-
// 用户不存在则创建
52-
// TODO
53-
if (!existUser) {
54-
return null;
55-
}
42+
const githubHandler = async (ctx, { profile }) => {
43+
const email = profile.emails && profile.emails[0] && profile.emails[0].value;
44+
const existUser = await ctx.service.user.getUsersByQuery({
45+
githubId: profile.id,
46+
});
5647

57-
// 用户存在,更新字段
58-
// existUser.loginname = profile.username;
59-
existUser.githubId = profile.id;
60-
existUser.email = email || existUser.email;
61-
existUser.avatar = profile._json.avatar_url;
62-
existUser.githubUsername = profile.username;
63-
existUser.githubAccessToken = profile.accessToken;
64-
await existUser.save();
48+
// 用户不存在则创建
49+
// TODO
50+
if (!existUser) {
51+
return null;
52+
}
6553

66-
return existUser;
67-
};
54+
// 用户存在,更新字段
55+
// existUser.loginname = profile.username;
56+
existUser.githubId = profile.id;
57+
existUser.email = email || existUser.email;
58+
existUser.avatar = profile._json.avatar_url;
59+
existUser.githubUsername = profile.username;
60+
existUser.githubAccessToken = profile.accessToken;
61+
await existUser.save();
6862

69-
const handle = user => {
70-
let handler;
71-
switch (user.provider) {
72-
case 'github':
73-
handler = githubHandler;
74-
break;
75-
default:
76-
handler = localHandler;
77-
}
78-
return handler(user);
79-
};
80-
return handle(user);
63+
return existUser;
64+
};
65+
66+
app.passport.verify(async (ctx, user) => {
67+
ctx.logger.debug('passport.verify', user);
68+
let handler;
69+
switch (user.provider) {
70+
case 'github':
71+
handler = githubHandler;
72+
break;
73+
default:
74+
handler = localHandler;
75+
}
76+
return handler(ctx, user);
8177
});
8278
};

0 commit comments

Comments
 (0)