forked from zheng1/nodebb-plugin-sso-wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibrary.js
executable file
·100 lines (87 loc) · 2.46 KB
/
library.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
var user = module.parent.require('./user'),
meta = module.parent.require('./meta'),
db = module.parent.require('../src/database'),
passport = module.parent.require('passport'),
passportWechat = require('passport-wechat').Strategy,
fs = module.parent.require('fs'),
path = module.parent.require('path'),
nconf = module.parent.require('nconf'),
async = module.parent.require('async');
//var constants = module.parent.require('../plugin_configs/sso_wechat_constants');
var Wechat = {};
Wechat.getStrategy = function(strategies, callback) {
passport.use(new passportWechat({
appID: "wx7c5fd23380cfe8d0",
appSecret: "035cb32cd44e88c2a317b8e4283c8fd8",
client:"wechat",
scope:"snsapi_userinfo",
callbackURL: nconf.get('url') + '/auth/wechat/callback'
}, function(accessToken, refreshToken, profile, done) {
Wechat.login(profile.id, profile.displayName, function(err, user) {
if (err) {
return done(err);
}
done(null, user);
});
}));
strategies.push({
name: 'wechat',
url: '/auth/wechat',
callbackURL: '/auth/wechat/callback',
icon: 'fa-weixin',
scope: ''
});
callback(null, strategies);
};
Wechat.login = function(wxid, handle, callback) {
Wechat.getUidByWechatId(wxid, function(err, uid) {
if (err) {
return callback(err);
}
if (uid !== null) {
// Existing User
callback(null, {
uid: uid
});
} else {
// New User
user.create({
username: handle
}, function(err, uid) {
if (err) {
return callback(err);
}
// Save wechat-specific information to the user
user.setUserField(uid, 'wxid', wxid);
db.setObjectField('wxid:uid', wxid, uid);
callback(null, {
uid: uid
});
});
}
});
};
Wechat.getUidByWechatId = function(wxid, callback) {
db.getObjectField('wxid:uid', wxid, function(err, uid) {
if (err) {
return callback(err);
}
callback(null, uid);
});
};
Wechat.deleteUserData = function(uid, callback) {
async.waterfall([
async.apply(user.getUserField, uid, 'wxid'),
function(oAuthIdToDelete, next) {
db.deleteObjectField('wxid:uid', oAuthIdToDelete, next);
}
], function(err) {
if (err) {
winston.error('[sso-wechat] Could not remove OAuthId data for uid ' + uid + '. Error: ' + err);
return callback(err);
}
callback(null, uid);
});
};
module.exports = Wechat;