Skip to content

Commit 513d69a

Browse files
thonatosJacksonTian
authored andcommitted
feat: add passport support
1 parent f4edbf7 commit 513d69a

File tree

1 file changed

+61
-57
lines changed

1 file changed

+61
-57
lines changed

app.js

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

10-
const localHandler = async (ctx, { username, password }) => {
11-
const getUser = username => {
12-
if (username.indexOf('@') > 0) {
13-
return ctx.service.user.getUserByMail(username);
14-
}
15-
return ctx.service.user.getUserByLoginName(username);
16-
};
17-
const existUser = await getUser(username);
10+
app.passport.verify(async (ctx, user) => {
11+
ctx.logger.debug('passport.verify', user);
1812

19-
// 用户不存在
20-
if (!existUser) {
21-
return null;
22-
}
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);
2321

24-
const passhash = existUser.pass;
25-
const equal = tools.compare(passhash, password);
22+
// 用户不存在
23+
if (!existUser) {
24+
return null;
25+
}
2626

27-
// 密码不匹配
28-
if (!equal) {
29-
return null;
30-
}
27+
const passhash = user.pass;
28+
const equal = tools.compare(passhash, password);
3129

32-
// 用户未激活
33-
if (!existUser.active) {
34-
// 发送激活邮件
35-
return null;
36-
}
30+
// 密码不匹配
31+
if (!equal) {
32+
return null;
33+
}
3734

38-
// 验证通过
39-
return existUser;
40-
};
35+
// 用户未激活
36+
if (!existUser.active) {
37+
// 发送激活邮件
38+
return null;
39+
}
4140

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-
});
41+
// 验证通过
42+
return existUser;
43+
};
4744

48-
// 用户不存在则创建
49-
// TODO
50-
if (!existUser) {
51-
return null;
52-
}
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+
});
5350

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();
51+
// 用户不存在则创建
52+
// TODO
53+
if (!existUser) {
54+
return null;
55+
}
6256

63-
return existUser;
64-
};
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();
6565

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);
66+
return existUser;
67+
};
68+
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);
7781
});
7882
};

0 commit comments

Comments
 (0)