-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
61 lines (59 loc) · 1.56 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* eslint-disable strict */
const LocalStrategy = require('passport-local').Strategy;
module.exports = (app) => {
// Mount strategy
app.passport.use(
new LocalStrategy(
{
passReqToCallback: true,
},
(req, username, password, done) => {
// format user
const user = {
provider: 'local',
username,
password,
};
app.logger.info('%s %s get user: %j', req.method, req.url, user);
app.passport.doVerify(req, user, done);
}
)
);
// Process user information
app.passport.verify(async (ctx, user) => {
app.logger.info('verify', user);
const u = await ctx.service.user.validatorUser(user);
if (u) {
return await u.get();
}
ctx.logout();
throw {
message: '密码或手机错误',
status: 401,
};
});
app.passport.serializeUser(async (ctx, user) => {
// app.logger.info('serializeUser', user);
return user;
});
app.passport.deserializeUser(async (ctx, user) => {
// app.logger.info('deserializeUser', user);
// user = await ctx.model.User.findByPk(user.id);
return user;
});
// app.once('server', server => {
// // websocket
// });
// app.on('error', (err, ctx) => {
// // report error
// });
// app.on('request', ctx => {
// // log receive request
// });
// app.on('response', ctx => {
// // ctx.starttime is set by framework
// const used = Date.now() - ctx.starttime;
// // log total cost
// ctx.logger.info('hello', used);
// });
};