Skip to content

Commit 043b341

Browse files
committed
refactor: rewrite login by wechat / sms, put main logic in service
1 parent cf48f65 commit 043b341

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

app.js

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,16 @@ module.exports = app => {
1515
}));
1616

1717
app.passport.verify(async (ctx, user) => {
18-
let newUser;
1918
if (user.provider === 'sms') {
20-
const { mobile, captcha } = user;
21-
const isValid = await ctx.service.sms.checkCaptcha({ mobile, captcha });
22-
23-
if (!isValid) {
24-
ctx.throw(400, '验证码不正确', { code: 'WRONG_CAPTCHA', errors: { mobile, captcha } });
25-
}
26-
27-
const existsUser = await ctx.service.user.find({ mobile });
28-
if (existsUser) return existsUser;
29-
30-
newUser = await ctx.service.user.create({ mobile });
19+
return await ctx.service.user.loginBySMS(user);
3120
} else if (user.provider === 'wechat') {
32-
const wechatUser = {};
33-
[ 'openid', 'nickname', 'sex', 'city', 'province', 'country', 'headimgurl' ].forEach(key => {
34-
wechatUser['wechat_' + key] = user[key];
35-
});
36-
37-
const wechat_openid = wechatUser.wechat_openid;
38-
const existsUser = await ctx.service.user.find({ wechat_openid });
39-
if (existsUser) return existsUser;
40-
newUser = await ctx.service.user.create(wechatUser);
21+
return await ctx.service.user.loginByWechat(user);
4122
}
42-
43-
if (newUser) {
44-
// 新的用户,自动帮用户创建一个钱包
45-
ctx.service.wallet.create({ user_id: newUser.id, digiccy: 'ETH' });
46-
return newUser;
47-
}
48-
4923
ctx.throw(500, '无效的Passport Provider:' + user.provider, { code: 'INVALID_PASSPORT_PROVIDER', errors: user });
5024
});
5125

5226
app.passport.serializeUser(async (ctx, user) => {
53-
return user;
27+
return user.get(); // get() can get full data, toJSON() only returns non sensitive data
5428
});
5529

5630
app.passport.deserializeUser(async (ctx, user) => {

app/service/user.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@ class UserService extends Service {
88
return this.ctx.model.User.create(user);
99
}
1010

11+
// 短信验证码登录
12+
async loginBySMS({ mobile, captcha }) {
13+
const ctx = this.ctx;
14+
15+
const isValid = await ctx.service.sms.checkCaptcha({ mobile, captcha });
16+
if (!isValid) {
17+
ctx.throw(400, '验证码不正确', { code: 'WRONG_CAPTCHA', errors: { mobile, captcha } });
18+
}
19+
20+
const exist_user = await ctx.model.User.find({ where: { mobile } });
21+
console.log(exist_user);
22+
if (exist_user) return exist_user;
23+
24+
25+
return ctx.model.User.create({ mobile });
26+
}
27+
28+
// 微信登录
29+
async loginByWechat(info) {
30+
const ctx = this.ctx;
31+
32+
const wechat_info = {};
33+
['openid', 'nickname', 'sex', 'city', 'province', 'country', 'headimgurl'].forEach(key => {
34+
wechat_info['wechat_' + key] = info[key];
35+
});
36+
let exist_user = await ctx.model.User.find({ where: { wechat_openid: wechat_info.wechat_openid } });
37+
let user;
38+
39+
if (exist_user) {
40+
// 更新微信的相关资料
41+
user = await exist_user.update(wechat_info);
42+
} else { // 首次登录,创建新用户
43+
user = await ctx.model.User.create(wechat_info);
44+
}
45+
46+
return user;
47+
}
48+
1149
async getByAddress(address) {
1250
// TODO: address统一变小写,检查address是否合法
1351
// ctx.throw(400, '无效地址', { code: 'INVALID_ADDRESS', error: { address } });

0 commit comments

Comments
 (0)