Skip to content

Commit b6cf1c5

Browse files
thonatosJacksonTian
authored andcommitted
refactor: move tools to helper (#72)
1 parent 123f94f commit b6cf1c5

File tree

6 files changed

+21
-29
lines changed

6 files changed

+21
-29
lines changed

app.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
const tools = require('./app/common/tools');
4-
53
module.exports = app => {
64
if (app.config.debug) {
75
app.config.coreMiddleware.unshift('less');
@@ -23,7 +21,7 @@ module.exports = app => {
2321

2422
const passhash = existUser.pass;
2523
// TODO: change to async compare
26-
const equal = tools.bcompare(password, passhash);
24+
const equal = ctx.helper.bcompare(password, passhash);
2725
// 密码不匹配
2826
if (!equal) {
2927
return null;

app/common/tools.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/controller/sign.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ const utility = require('utility');
55
const uuid = require('uuid');
66
const Controller = require('egg').Controller;
77

8-
const tools = require('../common/tools');
9-
108
class SignController extends Controller {
119
async showLogin() {
1210
const { ctx } = this;
@@ -34,7 +32,7 @@ class SignController extends Controller {
3432
msg = '信息不完整。';
3533
} else if (loginname.length < 5) {
3634
msg = '用户名至少需要5个字符。';
37-
} else if (!tools.validateId(loginname)) {
35+
} else if (!ctx.helper.validateId(loginname)) {
3836
msg = '用户名不合法。';
3937
} else if (!validator.isEmail(email)) {
4038
msg = '邮箱不合法。';
@@ -68,7 +66,7 @@ class SignController extends Controller {
6866
return;
6967
}
7068

71-
const passhash = tools.bhash(pass);
69+
const passhash = ctx.helper.bhash(pass);
7270

7371
// create gravatar
7472
const avatarUrl = service.user.makeGravatar(email);
@@ -203,7 +201,7 @@ class SignController extends Controller {
203201
});
204202
return;
205203
}
206-
const passhash = tools.bhash(psw);
204+
const passhash = ctx.helper.bhash(psw);
207205
user.pass = passhash;
208206
user.retrieve_key = null;
209207
user.retrieve_time = null;

app/controller/user.js

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

33
const _ = require('lodash');
44
const utility = require('utility');
5-
const tools = require('../common/tools');
65
const validator = require('validator');
76
const Controller = require('egg').Controller;
87

@@ -236,12 +235,12 @@ class UserController extends Controller {
236235
}
237236

238237
const user = await service.user.getUserById(ctx.user._id);
239-
const equal = tools.bcompare(oldPass, user.pass);
238+
const equal = ctx.helper.bcompare(oldPass, user.pass);
240239
if (!equal) {
241240
return showMessage('当前密码不正确。', user);
242241
}
243242

244-
const newPassHash = tools.bhash(newPass);
243+
const newPassHash = ctx.helper.bhash(newPass);
245244
user.pass = newPassHash;
246245
await user.save();
247246
return showMessage('密码已被修改。', user, true);

app/extend/helper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const MarkdownIt = require('markdown-it');
44
const validator = require('validator');
55
const jsxss = require('xss');
66
const moment = require('moment');
7+
const bcrypt = require('bcryptjs');
78

89
moment.locale('zh-cn'); // 使用中文
910

@@ -99,3 +100,15 @@ exports.ago = function(date) {
99100

100101
return date.fromNow();
101102
};
103+
104+
exports.validateId = str => {
105+
return /^[a-zA-Z0-9\-_]+$/i.test(str);
106+
};
107+
108+
exports.bhash = str => {
109+
return bcrypt.hashSync(str, 10);
110+
};
111+
112+
exports.bcompare = (str, hash) => {
113+
return bcrypt.compareSync(str, hash);
114+
};

test/app/controller/user.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
const tools = require('../../../app/common/tools');
43
const { app, assert } = require('egg-mock/bootstrap');
54

65
describe('test/app/controller/user.test.js', () => {
@@ -13,7 +12,7 @@ describe('test/app/controller/user.test.js', () => {
1312
ctx = app.mockContext();
1413
loginname = `user_loginname_${Date.now()}`;
1514
email = `${loginname}@test.com`;
16-
user = await ctx.service.user.newAndSave('name', loginname, tools.bhash('pass'), email, 'avatar_url', 'active');
15+
user = await ctx.service.user.newAndSave('name', loginname, ctx.helper.bhash('pass'), email, 'avatar_url', 'active');
1716
assert(user.loginname === loginname);
1817
});
1918

@@ -111,7 +110,7 @@ describe('test/app/controller/user.test.js', () => {
111110
assert(res3.status === 200);
112111

113112
const savedUser = await ctx.service.user.getUserById(user._id);
114-
const equal = tools.bcompare('newpass', savedUser.pass);
113+
const equal = ctx.helper.bcompare('newpass', savedUser.pass);
115114
assert(equal === true);
116115
});
117116
});

0 commit comments

Comments
 (0)