-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
53 lines (49 loc) · 1.32 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
const MemoryFileWorker = require('./lib/MemoryFileWorker');
module.exports = app => {
console.log('worker enter');
app.messenger.on('egg-ready', () => {
console.log('egg-ready from worker');
if (app.config.env === 'local') {
app.memoryFileWorker = new MemoryFileWorker(app);
}
});
app.passport.verify(async (ctx, user) => {
let userDoc;
if (user.provider) {
ctx.logger.info('verify by OAuth:' + user.provider);
//1. OAuth by Github
const matchConditions = {
uid: user.id,
provider: user.provider
};
// 查找授权文档
userDoc = await ctx.model.User.findOne(matchConditions);
// 查找用户文档
if (!userDoc) {
// 创建匿名用户文档
const conditions = {
provider: user.provider,
username: user.name,
email: user.profile._json.email,
avatar: user.photo,
github: user.profile.profileUrl,
uid: user.id
};
userDoc = await ctx.model.User.create(conditions);
}
}
if (!userDoc) {
ctx.logger.info('user not found');
}
return userDoc;
});
app.passport.serializeUser(async (ctx, user) => {
// ctx.logger.info('serializeUser');
return user._id;
});
app.passport.deserializeUser(async (ctx, userId) => {
// ctx.logger.info('deserializeUser:' + userId);
const userDoc = await ctx.model.User.findById(userId);
return userDoc;
});
}