-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
34 lines (28 loc) · 1022 Bytes
/
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
'use strict';
const LocalStrategy = require('passport-local').Strategy;
module.exports = app => {
const config = app.config.passportSMS || {};
config.passReqToCallback = true;
app.passport.use('sms', new LocalStrategy(config, (req, username, password, done) => {
const user = {
provider: 'sms',
mobile: username,
captcha: password,
};
app.passport.doVerify(req, user, done);
}));
app.passport.verify(async (ctx, user) => {
if (user.provider === 'sms') {
return await ctx.service.user.loginBySMS(user);
} else if (user.provider === 'wechat') {
return await ctx.service.user.loginByWechat(user);
}
ctx.throw(500, '无效的Passport Provider:' + user.provider, { code: 'INVALID_PASSPORT_PROVIDER', errors: user });
});
app.passport.serializeUser(async (ctx, user) => {
return user.get(); // get() can get full data, toJSON() only returns non sensitive data
});
app.passport.deserializeUser(async (ctx, user) => {
return user;
});
};